【发布时间】:2016-05-31 01:52:58
【问题描述】:
我在 node.js atm 中编码。我需要创建一个动态变量并调用它。
例如:
username = 'im_a_user';
global['ws[' + username + ']'] = ws; //(yes, i want to store the connection with ws module)
但是
ws[im_a_user].send('blabla');
不起作用并且节点关闭。所以我想知道global['ws[' + username + ']'] 在调试时的样子。
你知道我如何打印它吗?或者更好的是,为什么 ws[im_a_user].send('blabla'); 中的 im_a_user 没有定义?
感谢您的宝贵时间!
【问题讨论】:
-
im_a_user不是你要使用的变量名,用引号括起来,或者使用之前定义的变量username。 -
facepalm - 引号......现在一切都说得通了:D!它对我有用。你不知道我有多开心。非常感谢 - 我的意思是很多 xD
-
此外,点符号也可以使用:
ws.im_a_user.send(...);。 -
再次感谢您的信息!非常感谢:)
标签: javascript node.js variables dynamic equals