JavaScript learning Essay (ii) prototype prototype

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.