Created
December 17, 2011 17:12
-
-
Save richardanaya/1490781 to your computer and use it in GitHub Desktop.
Javascript Prototypical Object
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
| //This file is mean to help show how javascript classes are put together. | |
| //Parent Class constructor | |
| var A = function(foo) { | |
| //member variable | |
| this.foo = foo; | |
| }; | |
| //member function | |
| A.prototype.getSecret = function() { | |
| return this.foo; | |
| }; | |
| //Child Class constructor | |
| var B = function(foo, bar) { | |
| //Call parent constructor with arguments | |
| A.call(this,foo); | |
| //member variable | |
| this.bar = bar; | |
| }; | |
| //define parent child relationship | |
| B.prototype = Object.create(A.prototype); | |
| //override a member function | |
| B.prototype.getSecret = function() { | |
| return this.foo+this.bar; | |
| }; | |
| var A = new A('hello'); | |
| A.getSecret(); //should be 'hello' | |
| var B = new B('hello','world'); | |
| B.getSecret(); //should be 'helloworld' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment