Skip to content

Instantly share code, notes, and snippets.

@qetr1ck-op
Created September 27, 2015 22:12
Show Gist options
  • Save qetr1ck-op/fde34608e4532f82d166 to your computer and use it in GitHub Desktop.
Save qetr1ck-op/fde34608e4532f82d166 to your computer and use it in GitHub Desktop.
// 1. constructor
function Animal(name) {
this.name = name;
this.speed = 0;
}
// 2. methods in prototype
Animal.prototype.run = function(speed) {
this.speed += speed;
console.log(`${this.name} runnig, speed is ${this.speed}`);
};
Animal.prototype.stop = function() {
this.speed = 0;
console.log(`${this.name} стоит`);
};
var animal = new Animal('Banny');
console.log( animal.speed ); // 0, from __proto__
animal.run(5); // Banny runnig, speed is 5
animal.run(5); // Banny runnig, speed is 10
animal.stop(); // Banny runnig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment