javascript - Creating object dynamically using Object.create() -


i have scenario, need create objects dynamically.

in example, object meta contains name constructor function used during initialization object.create().

at moment using following code, able create objects dynamically property name not defined.

i need property on result;

what wrong in script? know better way achieve same result?

      (function () {              var costructors = {                  a: function () {                      this.name = 'a';                      console.log(this.name);                  },                  b: function () {                      this.name = 'b';                      console.log(this.name);                  },                  c: function () {                      this.name = 'c';                      console.log(this.name);                  }              },              meta = {                  a: true,                  b: true,                  c: true,              },              result = [];              function createobjs() {                  object.keys(meta).foreach(function (type) {                      var obj = object.create(costructors[type].prototype);                      result.push(obj);                  }.bind(this));              }              createobjs.call(this);              console.log(result);          })();

you haven't defined prototype of constructors, you're not creating name in instances, since you're creating object prototype, not constructor. try

object.create(constructors[type]) 

Comments