【发布时间】:2014-08-20 07:27:44
【问题描述】:
示例代码为:
// 4. execute tower.init latter read from bellow (1)
tower={
init:function(){ console.log('horee im here now, finaly after a day code');}
}
// 3. app object
app ={
publish:function(what,data){
data.nextModule.init();
}
}
// 2. then call this funct
var fires = function(){
return app.publish('subscriber',{ nextModule : 'tower'});
}
// 1. execute this first
fires();
解释问题
- 当我开火
fires()1. - 2.isfine
app.publish('text',{nextModule:'tower'); - 绕过到 3.
app.publish(text,data)
我的问题是
我想将data.nextmodule --> 转换为对象或函数
然后调用塔模块.init()
data.nextModule.init() 无法执行,因为nextModule 是String
如何让这段代码像这样运行
data.'tower'.init();
我已阅读此参考资料
豪沙尔特的回答 converting an object to a string !不是将字符串转换为对象
node js 可以吗?
我们可以像JSON.stringify(data) 一样转为反对吗?
终端更新错误
throw err;
^
TypeError: Object tower has no method `init`
【问题讨论】:
-
Node.js 使用 Chrome 的 V8 javascript 引擎,该引擎实现了 ECMA-262 中指定的 ECMAScript。所以是的,如果你可以在浏览器中完成,你可以在 Node 中完成。
-
抛出错误
data["nextModule"].init();typeError: Object tower has no method init..
标签: javascript node.js object