Skip to content

Instantly share code, notes, and snippets.

@AbraaoAlves
Created November 8, 2011 17:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbraaoAlves/1348500 to your computer and use it in GitHub Desktop.
Save AbraaoAlves/1348500 to your computer and use it in GitHub Desktop.
Simple example of inheritance, overwrite and override with only native javascript resources. Obs: This example use javascript 1.8.5 especification.
function A(){} /* constructor */
A.prototype = { /* especification of prop and methods of the class A */
num : 5,
name : 'test',
action : function(){ /*base implementation*/},
alert
};
function B(){ /* constructor */
/* called base constructor:*/
A.call(this);
}
B.prototype = Object.create(A.prototype); /* Object.create is suported by: Chrome5+, Firefox4+, IE9+, Safari5+ */
B.prototype.num = 20; /* overwrite prop*/
B.prototype.action = function(base){ /* overried method */
return function() {
/* own implementation */
base();
};
}(A.prototype.action);
B.prototype.name = function(base){ /* overried prop */
return base + " overload";
}(A.prototype.name);
/* using */
var a = new A();
var b = new B();
console.log(a); /*show this: {num: 5 , name: "test", action: function() {/* ... */}} */
console.log(b); /*show this: {num: 20, name: "test overload", action: function() {/* ... */}} */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment