javascript 声明没有值的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15523638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declaring variables without a value
提问by Curt
I wish to create a Javascript object with nested variables where some of the variables will not have a default value specified.
我希望创建一个带有嵌套变量的 Javascript 对象,其中一些变量没有指定默认值。
I currently have this:
我目前有这个:
var globalVarA = "foo";
var globalVarB = "bar";
var globalVarC;
function myFunction(){
//do something
}
I wish to change this to:
我希望将其更改为:
var myObject = {
GlobalVars: {
globalVarA: "foo",
globalVarB: "bar",
globalVarC
},
myFunction: function(){
//do something
}
}
However I get the following error:
但是我收到以下错误:
Expected ':'
预期的 ':'
How can I declare this variable without a value?
如何在没有值的情况下声明此变量?
Is this the best practice or is there an alternative better solution?
这是最佳做法还是有其他更好的解决方案?
回答by Benjamin Gruenbaum
If you want to define the variables as properties of GlobalVars
you have to explicitly assign undefined
to them
如果要将变量定义为属性,GlobalVars
则必须显式分配undefined
给它们
GlobalVars: {
globalVarA: "foo",
globalVarB: "bar",
globalVarC: undefined
},
Your code contained invalid notation, when using object literal notation you must specify a value for each variable. This is unlike other notations like XML or formParams where you can declare a attribute/property without setting it.
您的代码包含无效符号,当使用对象文字符号时,您必须为每个变量指定一个值。这与 XML 或 formParams 等其他符号不同,您可以在其中声明属性/属性而无需设置它。
Note, that in my solution undefined variables do get a default value - that value is just the primitive value undefined
which is what is returned by default for variables that were not defined.
请注意,在我的解决方案中,未定义的变量确实获得了一个默认值——该值只是原始值undefined
,它是未定义的变量默认返回的值。
However, your variable doesget defined if you do globalVarC: undefined
但是,如果您这样做,您的变量确实会被定义globalVarC: undefined
GlobalVars.globalVarC;//undefined
GlobalVars.globalVarX;//undefined
GlobalVars.hasOwnProperty("globalVarC");//true
GlobalVars.hasOwnProperty("globalVarX");//false
From the spec:
从规范:
4.3.9 undefined value:
primitive value used when a variable has not been assigned a value.
4.3.9 未定义值:
未为变量赋值时使用的原始值。
回答by Arun P Johny
You need to specify a value for globalVarC
else the javascript syntax for json will be wrong, since you do not have any value to initialize you can give the value undefined.
您需要指定一个值,globalVarC
否则 json 的 javascript 语法将是错误的,因为您没有任何要初始化的值,您可以给出值undefined。
undefined
is a global variable with primitive value undefined.
undefined
是一个具有原始值undefined的全局变量。
var myObject = {
GlobalVars: {
globalVarA: "foo",
globalVarB: "bar",
globalVarC: undefined
},
myFunction: function(){
//do something
}
}
回答by Ja?ck
You will want the undefined
primitive value to be the default value:
您将希望undefined
原始值成为默认值:
var myObject = {
GlobalVars: {
globalVarA: "foo",
globalVarB: "bar",
globalVarC: void 0
},
myFunction: function(){
//do something
}
}
The void 0
expression gives the value of undefined
, regardless of whether undefined
was "rerouted".
的void 0
表达产生的值undefined
,而不管是否undefined
是“重新路由”。