JavaScript three types of methods:
1, class method, 2, Object method, 3, prototype method, attention three similarities and differences
Cases:
1 functionpeople (name) {2 This. name=name;3 //Object Methods4 This. Introduce=function(){5Console.log (' My Name is ' + This. Name);6 }7 }8 9 //class MethodTenpeople.run=function(){ OneConsole.log (' I Can Run '); A } - - //Prototyping Methods thePeople.prototype.introducechinese=function(){ -Console.log (' My name is ' + This. Name); - } - + //Test - varp=NewPeople (' Tom ')); + A p.introduce (); at People.run (); -P.introducechinese ();
JavaScript Object type prototype reference, remember to assign the prototype of an object to an object prototype, which is not an inheritance relationship, but rather cloning, and the existence of conflicting functions or properties, preserving their own functions and properties
Example 1,
1 functionBaseClass () {2 This. showmsg=function(){3Alert (' Baseclass::showmsg ');4 };5 }6 7 functionExtendclass () {8 9 }Ten OneExtendclass.prototype=NewBaseClass (); A - varExtend=NewExtendclass (); -Extend.showmsg ();//Eject baseclass::showmsg
Example 2,
1 functionBaseClass () {2 This. showmsg=function(){3Alert (' Bassclass::showmsg ');4 };5 }6 7 functionExtendclass () {8 This. showmsg=function(){9Alert (' Extendclass::showmsg ');Ten }; One } A -Extendclass.prototype=NewBassclass (); - the varExtend=NewExtendclass (); -Extend.showmsg ();//Eject extendclass::showmsg
Function run will go to the function of the main body to find, if found to run, can not find the prototype to find the function. Or it can be understood that prototype does not clone a function with the same name.
JavaScript learning Essay (ii) prototype prototype