I want to accomplish the following so that I can prototype a property of an object prototype. It's difficult to explain, so it's best illustrated with some code. The following code gives me undefined for this.name in the console. This is due to the failure of binding. But how can I accompish this, so that it works as expected:
var User = function(name) {
this.name = name;
};
User.prototype.actions = {};
User.prototype.actions.eats = function(what) {
console.log(this.name + ' eats ' + what);
};
User.prototype.actions.eats = function(what) {
console.log(this.name + ' eats ' + what);
};
var Member = function(name) {
User.call(this,name);
};
Member.prototype = Object.create(User.prototype);
Member.prototype.constructor = Member;
Member.prototype.actions.sleeps = function(time) {
console.log(this.name + ' sleeps ' + time);
}
var user = new User('john');
var member = new Member('steward');
user.actions.eats('fries');
member.actions.eats('potatoes');
member.actions.sleeps('3 hours');