這個例子是我在網上看到的例子:
<script type="text/javascript">
var objectList2 = new Array();
function WorkMate(name, age) {
this.name = name;
var _age = age;
this.age = function() { //我實在是沒有看懂這里為什么要添加這樣一個方法
if(!arguments) { //如果沒有實參傳入
_age = arguments[0]; //那_age的值為實參的第一個的值 **沒有實參傳入,哪來的第一個值?**
} else {
return _age;
}
}
}
objectList2.push(new WorkMate('jack', 20));
objectList2.push(new WorkMate('tony', 25));
objectList2.push(new WorkMate('stone', 26));
objectList2.push(new WorkMate('mandy', 23));
//按年齡從小到大排序
objectList2.sort(function(a, b) {
return a.age() - b.age();
});
for(var i = 0; i < objectList2.length; i++) {
document.writeln('<br />age:' + objectList2[i].age() + ' name:' + objectList2[i].name);
}
</script>
當中這段我備注的我不知道我理解的對嗎 ,麻煩大神幫我看一下,中間arguments那里怎么理解,還有這個方法到底有什么用呢?謝謝
認證高級PHP講師
function a(){
console.log(!arguments);
}
a()//false
a(1)//false
那個if判斷貌似沒鳥用……arguments是function必備的,不管你有沒有傳入參數(shù)。