Skip to content

Instantly share code, notes, and snippets.

@SirSerje
Last active September 24, 2018 13:18
Show Gist options
  • Save SirSerje/6d1e6552a2262995a68733797bc67787 to your computer and use it in GitHub Desktop.
Save SirSerje/6d1e6552a2262995a68733797bc67787 to your computer and use it in GitHub Desktop.
'barking dog'
function Animal(name) {
this.name = name
}
Animal.prototype.getName = function() {
return this.name
}
function Dog(name) {
Animal.apply(this, arguments)
}
Dog.prototype = Object.create(Animal.prototype)
Dog.prototype.constructor = Dog
Dog.prototype.bark = function() {
return 'Dog ' + this.name + ' is barking'
}
const dog = new Dog('Aban')
console.log( dog.getName() === 'Aban' )
console.log( dog.bark() === 'Dog Aban is barking' );
class Animal {
constructor(name) {
this.name = name
}
getName() {
return this.name
}
}
class Dog extends Animal {
bark() {
return `Dog ${this.name} is barking`;
}
}
const dog = new Dog('Aban')
console.log( dog.getName() === 'Aban' )
console.log( dog.bark() === 'Dog Aban is barking' );
@SirSerje
Copy link
Author

//Dog.prototype.constructor = Dog я могу запустить пример без этой строки. Судя по всему Object.create уже делает екземпляр

@romanmtb
Copy link

это желательно, но не обязательно, это не ошибка

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment