// global scope
var sayMyFavoriteColor = function(adj){
return 'My ' + adj + ' color is ' + this.favoriteColor; + '!'
};
var callFnTest = function (opts) {
return sayMyFavoriteColor.call(this, opts);
};
this.favoriteColor = 'Brown'; //adding a global variable to window
i.e. var favoriteColor = 'Brown'
callFnTest('most disliked')
"My most disliked color is Brown"
我的问题是因为我们正在传递 this
调用它是指向窗口,对吗?
最佳答案
它没有改变。仅当使用类的实例时才能使用 this 关键字。基本上,如果您调用 a.whateverFunction()
,a
就是 this
。
关于javascript - 当call和apply嵌套在函数中时, "this"关键字指向什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41618181/