【发布时间】:2015-02-17 23:41:51
【问题描述】:
我在模块(节点)内有以下设置,但这可能适用于任何地方。
function Storefront(){
this.list = 'List';
}
Storefront.prototype = {
categories : {
get : function(){
return this.list + ' here!';
}
}
}
module.exports = function(){
if(!(this instanceof Storefront)) { return new Storefront(); }
}
稍后,我在另一个模块中将对象实例化为sfront 并调用sfront.categories.get();。
通常,只需在函数内部执行var self = this;,在其中调用子函数,然后引用self,这很容易。由于显而易见的原因,这不起作用:引用“this”的对象在另一个对象内部。我不太确定如何将父对象 Storefront 的上下文传递到子对象的(类别)子属性函数“get”中。现在拉起来是'undefined',这是可以理解的。
【问题讨论】:
标签: javascript node.js function object this