简体   繁体   English

声明没有值的变量

[英]Declaring variables without a value

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? 这是最佳做法还是有其他更好的解决方案?

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的原始值,这是默认情况下undefined的变量返回的值。

However, your variable does get 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: 4.3.9未定义的值:

primitive value used when a variable has not been assigned a value. 未赋值变量时使用的原始值。

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 is a global variable with primitive value undefined . undefined是一个全局变量,原始值未定义

var myObject = {
  GlobalVars: {
     globalVarA: "foo",
     globalVarB: "bar",
     globalVarC: undefined
  },
  myFunction: function(){
     //do something
  }
}

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是否被“重新路由”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM