gpt4 book ai didi

javascript - javascript 中的范围说明

转载 作者:行者123 更新时间:2023-11-29 22:05:14 25 4
gpt4 key购买 nike

<分区>

简单的问题。为什么我们要设置 that = this?如果我们不这样做,我们就在全局范围内……但为什么呢?

var myObj = {

specialFunction: function () {

},

anotherSpecialFunction: function () {

},

getAsyncData: function (cb) {
cb();
},

render: function () {
var that = this;
this.getAsyncData(function () {
// this now refers to global scope....why?
that.specialFunction();
that.anotherSpecialFunction();
});
}
};

myObj.render();

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com