我正在尝试基于数组参数在类中创建属性。你怎么能做到这一点?
请参阅以下内容,以便您了解我正在尝试做的事情:
function Person(colors) {  
    this.color = (function () {
        this.color = new Object();
        for (q=0; q<colors.length; ++q) {
            var r;
            switch (q) {
                case 0: r = "favorite"; break;
                case 1: r = "likes"; break;
                case 2: r = "hates"; break;
                default: r = q; break;
            }            
            this.color.r = colors[q];
        }
    }).call(this);
}
var people = {
    george: new Person(["blue", "yellow", "green"]),
    bob: new Person(["green", "purple", "white"])
};
console.log(people.george.color.favorite);
基本上我正在尝试做:
this.color = {
    favorite: colors[0],
    likes: colors[1],
    hates: colors[2]
};
使用 for 循环。有任何想法吗?
顺便说一句,我不确定是否说“this.color = new Object();” this.color 内部实际上是有效的,这只是我尝试过的。你还能怎么做?