假设我有这个代码:
var m =
{
init: function(num, num2, num3)
{
this.num = num;
this.num2 = num2;
this.num3 = num3;
}
};
var t =
{
create: function()
{
var obj = Object.create(m);
obj.init(1,2,3);
}
};
t.create();
console.log(obj)
执行此代码时,我收到此错误:
obj 未定义
我怎样才能obj
在方法之外工作create
?
更改您的create
函数以返回obj
. 然后,你可以做var obj = t.create()
。
这是完整的代码:
var m =
{
init: function(num, num2, num3)
{
this.num = num;
this.num2 = num2;
this.num3 = num3;
}
};
var t =
{
create: function()
{
var obj = Object.create(m);
obj.init(1,2,3);
return obj;
}
};
var obj = t.create();
console.log(obj)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句