попробуйте это:
function tomatoe(name, owner) {
//make a reference to this with self
var self = this;
$('<div>').click(self.squish).appendTo(myElement).text('I\'m a happy tomatoe called ' + name);
this.name = name;
this.dead = false;
this.owner = owner;
this.squish = function() {
console.log('Oh my Google, you killed ' + this.name + '!');
this.dead = true;
this.owner.sad = true;
};
}
что бы я сделал, если бы вы хотели изменчивости:
var tomato = {
name: null,
dead: null,
owner: null,
init: function(name, owner){
var self = this;
$('<div>').click(self.squish)
.appendTo(myElement).text('I\'m a happy tomatoe called ' + name);
this.name = name;
this.dead = false;
this.owner = owner;
return this;
},
squish: function(){
console.log('Oh my Google, you killed ' + this.name + '!');
this.dead = true;
this.owner.sad = true;
return this;
}
}
//to instantiate it:
tomato.init(name, owner);