Skip to content

Instantly share code, notes, and snippets.

@rexagod
Last active March 9, 2022 06:20
Show Gist options
  • Save rexagod/08b172fcc2eb7fc1f22fcb93793e1959 to your computer and use it in GitHub Desktop.
Save rexagod/08b172fcc2eb7fc1f22fcb93793e1959 to your computer and use it in GitHub Desktop.
Intro to John Resig's Class inheritance in JavaScript
var Person = Class.extend({
init: function(isDancing){
this.dancing = isDancing;
},
dance: function(){
return this.dancing;
}
});
var Ninja = Person.extend({
init: function(){
this._super( false );
},
dance: function(){
// Call the inherited version of dance()
return this._super();
},
swingSword: function(){
return true;
}
});
var p = new Person(true);
p.dance(); // => true
var n = new Ninja();
n.dance(); // => false
n.swingSword(); // => true
// Should all be true
p instanceof Person && p instanceof Class &&
n instanceof Ninja && n instanceof Person && n instanceof Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment