Skip to content

Instantly share code, notes, and snippets.

@graouts
Last active August 29, 2015 13:56
Show Gist options
  • Save graouts/9192008 to your computer and use it in GitHub Desktop.
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.
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