var obj = {
name : ["Dean Edwards", "Sam Stephenson", "Alex Russell", "Dean Tom"]
};
function addMethod(obj , name , fn){
var old2 = obj[name];
obj[name] = function(){
if(fn.length == arguments.length){
return fn.apply(this , arguments);
}
else if(typeof old2 === 'function'){
console.log(old2 == obj[name]); //这里为什么是不相等的呢?
return old2.apply(this , arguments);
}
};
}
addMethod(obj , "find" , function(){
return this.name;
});
addMethod(obj , "find" , function(firstname){
var ret = [];
for(var i = 0 ; i < this.name.length; i++){
if(this.name[i].indexOf(firstname) === 0){
ret.push(this.name[i]);
}
}
return ret;
});
addMethod(obj , "find" , function(firstname , lastname){
var ret = [];
for(var i = 0 ; i < this.name.length; i++){
if(this.name[i] === firstname + " " + lastname){
return ret.push(this.name[i]);
}
}
return ret;
});
console.log(obj.find());
console.log(obj.find('Dean'));
console.log(obj.find('Dean','Edwards'));
old2.apply(this , arguments);请问这段代码是怎么理解的呢,以及方法绑定的链条是怎么样的呢?请各位大神解答一下,谢谢啦!为什么可以向上调用old指向的上个方法。
橋本奈奈未
相关分类