Skip to content

Instantly share code, notes, and snippets.

@richardanaya
Created December 17, 2011 17:12
Show Gist options
  • Select an option

  • Save richardanaya/1490781 to your computer and use it in GitHub Desktop.

Select an option

Save richardanaya/1490781 to your computer and use it in GitHub Desktop.
Javascript Prototypical Object
//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