This prototype chain? For name placement purposes
Let's say mine Dog
.
function Dog(name) {
this.name = name;
}
I want some functions to be placed on some prototypes.
Dog.prototype.movement = {
this.run = this.name + 'is running',
this.jump = function() { console.log(this.name + 'is jummping');}
}
So I can call it like this:
var carl = new Dog('Car');
carl.movement.jump();
//Carl is jumping.
+3
source to share
1 answer
You were close, just need to be considered run
and jump
the properties of the object, not the target variables:
Dog.prototype = {
movement: function() {
return {
run: function() {
this.name + 'is running'
}.bind(this),
jump: function() {
console.log(this.name + ' is jummping');
}.bind(this)
}
}
}
var carl = new Dog('Car');
carl.movement().jump();
+3
source to share