Declaring variables without a value

后端 未结 3 1929
闹比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条回答
  •  Happy的楠姐
    2021-02-09 11:56

    You will want the undefined primitive value to be the default value:

    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".

提交回复
热议问题