开发者

JS: join()-ing an array of objects

Say I have an array of objects that I want to be able to access as hashes sometimes (to change their values, eh?) and sometimes print.

var test = { members : [] };
test.addMember = function(name, value) { 
    this.members[name] = { name : name, value : value }; 
    this.members[name].toString = function() { 
        return this.name + " has a " + this.value; };
};
test.toString = function() { 
    return this.members.join(" and ");
};
test.addMember("a", "a value");
test.addMember("b", "b value");
alert(test);

My Goal here is to have test.toString() return:

a has a value and b has b value

or what-have-you. I was reading on the MDN, and it seems that JavaScript 1.8.5 (or some subrevision) will have a join() that calls toString(). Am I stuck?

Thanks!

EDIT: Here's my FINAL design, with a modify and delete function included (in case anyone was curious)!

var test = { 
    members : [],
    modifyMember : function(name, value) {
        var index = this.members.indexOf(this.members[name]);
        if(index < 0) {
            return;
        }
        this.members[index].value = this.members[name].value = value;
    },
    addMember : function(name, value) { 
        if(this.members[name]) { 
            this.modifyMember(name, value);
            return;
        }
        this.members[name] = { 
            name : name,开发者_运维知识库 
            value : value,
            toString : function() { 
                return this.name + " has " + this.value; 
            },
        }; 
        this.members.push(this.members[name]);
    },
    removeMember : function(name) { 
        if(!this.members[name]) return;
        this.members.splice(this.members.indexOf(this.members[name]), 1);
        delete this.members[name];
    },
    toString : function() {
        return this.members.join(" AND ");
    } 
};


You are already expanding your Array with properties. Now, just don't forget to populate your Array. Use Array.push():

test.addMember = function(name, value) { 
    this.members[name] = { name : name, value : value }; 
    this.members.push(this.members[name]);
    ...
};


One way would be: (members is an object)

var test = { members : {} };
test.addMember = function(name, value) { 
    this.members[name] = { name : name, value : value }; 
    this.members[name].toString = function() { 
        return this.name + " has a " + this.value; };
};
test.toString = function() {
    var arr=[];
    for(var i in this.members) {
        if (this.members.hasOwnProperty(i)){
            arr.push(this.members[i].toString());
        }
    }
    return arr.join(" and ");
};
test.addMember("a", "a value");
test.addMember("b", "b value");
alert(test.toString());

And another way (members is an array):

var test = { members : [] };
test.addMember = function(name, value) { 
    var arr= { name : name, value : value }; 
    arr.toString = function() { 
        return this.name + " has a " + this.value; 
    };
    this.members.push(arr);
};
test.toString = function() { 
    return this.members.join(" and ");
};
test.addMember("a", "a value");
test.addMember("b", "b value");
alert(test.toString());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜