Last active
May 29, 2017 00:08
-
-
Save Alcotana/18e81cea70dd6de9aa9876273d591cb5 to your computer and use it in GitHub Desktop.
Classes-like functions example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// class | |
function Foo(who){ | |
// constructor | |
this.me = who; | |
this.species = 'fufel'; | |
// methods | |
Object.assign(Foo.prototype, { | |
identify(){ | |
return 'I am ' + this.me; | |
} | |
}); | |
} | |
// class | |
function Bar(who){ | |
// super(..) | |
Foo.call(this, who); | |
// extends | |
Object.setPrototypeOf( | |
Bar.prototype, Foo.prototype | |
); | |
// constructor | |
this.subtype = 'barashek'; | |
// methods | |
Object.assign(Bar.prototype, { | |
speak(){ | |
console.log('Hello, ' + this.identify()); | |
} | |
}); | |
} | |
a1 = new Foo('a1'); | |
b1 = new Bar('b1'); | |
console.log(a1); | |
console.log(b1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment