Last active
March 9, 2022 06:20
-
-
Save rexagod/08b172fcc2eb7fc1f22fcb93793e1959 to your computer and use it in GitHub Desktop.
Intro to John Resig's Class inheritance in JavaScript
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
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