JavaScript Object Literal表示法内部变量指向 [英] JavaScript Object Literal Notation Internal Variable Pointing
本文介绍了JavaScript Object Literal表示法内部变量指向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一系列变量。
我希望一个变量等于前一个变量。
例如:
I have an array of variables. And I want one variable to equal the previous one. For example:
var myVars = {
var1: "test",
var2: var1
};
alert(myVars.var2);
//输出:var1未定义
有什么想法吗?我确信这是某种可变范围限制。
我想听听。
提前致谢。
Any thoughts? I'm sure this is some sort of variable scope limitation. I would like to hear otherwise. Thanks in advance.
推荐答案
您不能引用相同的对象文字在不使用函数的表达式中,我建议你使用等效语法:
You cannot refer to the same object literal in an expression without using a function, I would recommend you to use the equivalent syntax:
var myVars = {};
myVars.var1 = "test",
myVars.var2 = myVars.var1;
这篇关于JavaScript Object Literal表示法内部变量指向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文