Automatically create object if undefined

后端 未结 12 1056
Happy的楠姐
Happy的楠姐 2020-12-07 13:07

Is there an easy way to automatically add properties to objects if they don\'t allready exist?

Consider the following example:

var test = {}
test.hel         


        
12条回答
  •  星月不相逢
    2020-12-07 13:58

    var test = {}
    if(!test.hasOwnProperty('hello')) {
        test.hello = {};
    }
    test.hello.world = "Hello World!"
    

提交回复
热议问题