Set undefined javascript property before read

后端 未结 3 419
执笔经年
执笔经年 2020-11-29 13:20
var tr={};
tr.SomeThing=\'SomeThingElse\';
console.log(tr.SomeThing); // SomeThingElse
console.log(tr.Other); // undefined

tr.get=function(what){
    if (tr.hasOwnP         


        
3条回答
  •  死守一世寂寞
    2020-11-29 13:49

    While this solution isn't exactly what you were looking for, a JavaScript implementation of python's collections.defaultdict class might help:

    var collections = require('pycollections');
    var dd = new collections.DefaultDict([].constructor);
    console.log(dd.get('missing'));  // []
    dd.get(123).push('yay!');
    console.log(dd.items()); // [['missing', []], [123, ['yay!']]]
    

提交回复
热议问题