【发布时间】:2013-03-19 14:10:47
【问题描述】:
我正在尝试使用这种模式创建一个“构造函数”:
function mything() {
var a, b, c;
...
return {
publicFunc: function() {
//access private vars here
}
};
}
//usage
mything1 = mything();
mything2 = mything();
问题是,我还想让它通过instanceof 测试:
assert(mything1 instanceof mything === true);
有没有办法做到这一点?使用常规构造函数将不起作用,因为原型函数无法访问私有变量。
【问题讨论】:
-
为了让
instanceof返回true,构造函数的prototype必须在实例的原型链中。
标签: javascript oop constructor