There has some tips about JS inheritance.

  • Class to inherit using: ChildClassName.prototype = new ParentClassName();
  • Need to reset the constructor property: ChildClassName.prototype.constructor = ChildClassName
  • Can overridden parent method using Function.Call()
  • JS does not support Protected Method.
  • Every object instance in JS has a property named ‘constructor’ that points to its parent class.
  • JS does not have ‘Super’ method or property. Instead it has Call() of Function object. That is Function.Call()
  • In Call() method the parameter must be ‘this’. Can add more parameters after ‘this’.

Pure Virtual Class(PVClass):

ClassName = {

MethodName : function(){ … }

}

To inherit this need to make some code as below:

  • ChildClassName.prototype = PVClassName
  • ChildClassName.prototype.parent = PVClassName

Generalized method to access both concrete and virtual class:

Function.prototype.inheritsFrom = function( parentClassOrObject ){ 
	if ( parentClassOrObject.constructor == Function ) 
	{ 
		//Normal Inheritance 
		this.prototype = new parentClassOrObject;
		this.prototype.constructor = this;
		this.prototype.parent = parentClassOrObject.prototype;
	} 
	else 
	{ 
		//Pure Virtual Inheritance 
		this.prototype = parentClassOrObject;
		this.prototype.constructor = this;
		this.prototype.parent = parentClassOrObject;
	}
	return this;
} 

ChildClassName.inheritsFrom(ParentClassName);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s