Skip to content

Instantly share code, notes, and snippets.

@Alcotana
Last active May 29, 2017 00:08
Show Gist options
  • Save Alcotana/18e81cea70dd6de9aa9876273d591cb5 to your computer and use it in GitHub Desktop.
Save Alcotana/18e81cea70dd6de9aa9876273d591cb5 to your computer and use it in GitHub Desktop.
Classes-like functions example
// 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