当call和apply嵌套在函数中时,指向的“this”关键字是什么?

时间:2017-01-12 16:23:08

标签: javascript this call

// 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来调用它指向窗口,对吗?

1 个答案:

答案 0 :(得分:0)

它没有改变。使用类的实例时,只能使用this关键字。基本上,如果您致电a.whateverFunction()athis