Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

觉得call和apply函数在我们编程中是鸡肋,大家怎么看

$
0
0

看了非常多的网络教程,基本上知道call的主要作用就是改变this指向 ,更好的理解就是改变了我们需要执行函数的上下文,把上下文变成我们指定的函数或则对象。

function name() {
    return {
        name:"timliu",
        sayName : function () {
        console.log("My name is " + this.name);
    }
}
}
name().sayName();	//My name is timliu

change = {
    name: "狼叔"
}
name().sayName.call(change);		//My name is 狼叔
name().sayName.apply(change);       //My name is 狼叔

Viewing all articles
Browse latest Browse all 14821

Trending Articles