Reputation: 372
I don't know if the question title makes sense, but one thing that I never learned was how to maintain control over objects created with the prototyping method. More specifically, objects that are not created via
var Joe = new Person('Joe');
Joe.sayName();
Joe.die();
But rather
var people = {};
var everyone = getAllPeopleFromSomewhere()
$.each(everyone, function(i,e){
people[i] = new Person(e.name);
});
function die(id){
people[id].die(); // obvious error
}
die(1);
I feel like I need another abstract system to manage an unknown amount of objects created in this way. But using an Id to reference them from an Object (or array) doesn't seem to be the way to go.
Upvotes: 0
Views: 13
Reputation: 735
Looking at your code, the problem that you're experiencing stems from this line, in two places:
people[i] = new Person(e.name).sayName();
This can be fixed in two ways: splitting up the call, or returning this
in the sayName function.
Person.prototype = {
sayName: function () {
$('#' + this.name).html(this.name);
return this;
},
die : function(){
$('#' + this.name).remove();
}
};
Upvotes: 1