Declaring variables without a value

后端 未结 3 1927
闹比i
闹比i 2021-02-09 11:12

I wish to create a Javascript object with nested variables where some of the variables will not have a default value specified.

I currently have this:

va         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-09 11:37

    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.

    undefined is a global variable with primitive value undefined.

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

提交回复
热议问题