如何从属性数组中实例化对象

时间:2017-05-10 08:43:09

标签: javascript oop constructor

我无法找到让这种事情发挥作用的方法:

// I want to achieve managing the structure of the object here and only here
var objectAPropertiesList = ["aProp", "bProp", "cProp"];

// Object with constructor
function ObjectA(properties) {
    objectAPropertiesList.forEach(function (property) {
        this[property] = properties[property];
    });
}
// instantiation
myObjectA = new ObjectA({"aProp": 1, "bProp": 2, "cProp": 3});

// keeps giving me '{}' !
console.log(JSON.stringify(myObjectA));

有人可以解释一下吗?

0 个答案:

没有答案