JS prototype
/*******************prototype*******************
* You may find prototype confusing while reading JS
* One having an understanding of Java superclass will be easily able to correlate
* prototype in JS is just a super class.
* If one says o is a protoype to p, it means the object p extends o(super class java).
* or can be interpreted as : o is a blueprint while p is the object made from blueprint o
* ok having said that, let dive deep into it:
* we already know function is javascript are the first class object or simply a class constructor (like java)
* for instance:
*/
function MyFuncClass() {//you may assume MyFuncClass a name of a class
this.myVarA = "first";
this.myVarB = "second";
}
//now similar to java, if we want this JS Class MyFuncClass to inherit some properties defined by its super class
//then we need to define the prototype of this class MyFuncClass.
//to define the prototype , one can define a new object which will have some properties
//and which inturn will be extended(inherited) by MyFuncClass:
var SuperClsObj = { supVarA: "superfirst", supVarB: "superSec" };
// we have define a new class(anonymous) object say, SuperClsObj
//which is having two properties (member variable like java).
MyFuncClass.prototype = SuperClsObj;//here we declared that MyFuncClass extends(inherit) SuperClsObj object
//Note : Also if you want to make a class extend super class, do before its instantiation
//now instantiate MyFuncClassObj class
var MyFuncClassObj = new MyFuncClass();//similar to java object creation
//here you can see we have easily created an object of class MyFuncClass(jS function constructor).
//and the newly created objected has two properties.
var MyFuncClassObj = new MyFuncClass();
console.log(MyFuncClassObj.supVarB);//will print superSec (super class property access)
//lets See another example where super class is a named super class, above example used anonymous super class.
function MySuperClass() {//you may assume MySuperClass , a named super class
this.suppA = "ValSuppA";
this.suppB = "ValSuppB";
}
var supObj = new MySuperClass();
console.log(supObj);//will print MySuperClass { suppA: 'SuppA', suppB: 'SuppB' }
function MyChildClass() {//you may assume MyChildClass a child class
this.chldA = "ChildA";
this.chldB = "ChildB";
}
MyChildClass.prototype = supObj;
var childObj = new MyChildClass();
console.log(childObj.suppA);//will print ValSuppA (super class property access)
//now lets print both child objects
console.log(MyFuncClassObj);//will print { myVarA: 'first', myVarB: 'second' }
console.log(childObj);//will print MySuperClass { chldA: 'ChildA', chldB: 'ChildB' }
//Some point to be noticed from above two print statements, that both have
//printed the class body of their class, one with anonymous super class name (blank) and class body of child
//another one with named class as super class name and class body of child. Thus it makes a point that
//when you define a class X prototype of class Y, class Y becomes the model of class X
//or in simple words it will be known by its prototype class X
//or class X is an iron mold(metal prototype) of shape rectangle from which class Y(iron frame) is made
Comments
Post a Comment