Last active
August 29, 2015 13:56
-
-
Save graouts/9192008 to your computer and use it in GitHub Desktop.
Example of very simple JavaScript inheritance using JS core features only, following the style used in the WebKit Web Inspector.
This file contains hidden or 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
function Bar(someParam) | |
{ | |
// Call the superclass's constructor | |
Foo.call(this, someParam); | |
// Bar-specific initialization follows… | |
this._property = null; | |
} | |
// Some constants. | |
Bar.Constants = { | |
Foo: "foo", | |
Bar: "bar", | |
Baz: "baz" | |
}; | |
Bar.staticMethod = function() | |
{ | |
// A static method | |
}; | |
Bar.prototype = { | |
constructor: Bar, | |
__proto__: Foo.prototype, | |
// Public | |
get someProperty() | |
{ | |
// a property accessor for .someProperty | |
return this._property; | |
}, | |
set someProperty(x) | |
{ | |
// a property setter for .someProperty | |
this._property = x; | |
}, | |
doSomething: function(someParam) | |
{ | |
// a public method, where you can easily call the superclass's implementation | |
return Foo.prototype.doSomething.call(this, someParam); | |
}, | |
// Protected | |
handleEvent: function(event) | |
{ | |
// a protected method, for instance an implementation of the DOM EventListener interface, | |
// can also be used for implementations of delegation protocols | |
}, | |
// Private | |
_doSomethingPrivate: function() | |
{ | |
// a private method | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment