出于某种奇怪的原因,当调用将 this
分配给 thisObj
的函数时,我收到错误:
TypeError: thisObj is undefined
这是我得到的:
function templateObject()
{
"use strict";
var thisObj = this;
function _loadBackgroundImages()
{
"use strict";
// something happens here
}
thisObj.initialise = function()
{
"use strict";
_loadBackgroundImages();
};
}
然后使用实例化调用该函数,如下所示:
var templateObj = templateObject();
templateObj.initialise();
无法弄清楚为什么会出现错误 - 有什么想法吗?
最佳答案
使用new
:
var templateObj = new templateObject();
使用 new
调用函数会将新创建的空对象作为 this
传递给函数,然后将其返回给 templateObj
。
关于javascript - OOP JavaScript - 此引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17928103/