javascript - 为什么我无法访问在对象的方法中创建的对象
问题描述
假设我有这个代码:
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)
推荐阅读
- google-chrome - 检查远程设备上的边缘导致 404 未找到
- ssl - 带有自签名证书的 SSL IOWebSocketChannel 使用颤振
- javascript - jQuery 在方法之前绝对需要 hide() 吗?
- python - 如何弥补丢失的引用来声明一个字段(numpy)?
- jquery - 使用复选框过滤数据表
- sql - 访问 SQL count() 查询
- nginx - 安装 hhvm 后,在 /usr/share 文件夹中找不到 hhvm 的文件夹
- javascript - 从dom中隐藏输入值属性
- regex - 如何在 Apache 2.4 if 语句中访问正则表达式反向引用
- amazon-web-services - 如何从 AWS Cloud9 部署站点/应用程序