Three ways in which JavaScript implements inheritance

Source: Internet
Author: User
Tags inheritance

First, the prototype chain inheritance

functionfunctionnew

The object child's prototype property points to an instance of the parent object, so that an instance of the child object is accessed through the prototype chain to the properties, methods, etc. defined by the parent object construction.

second, the use of apply, call method

JS in the call and apply can be inherited, the only point of a different parameter, Func.call (FUNC1,VAR1,VAR2,VAR3) corresponding to the application of the wording: Func.apply (Func1,[var1,var2,var3]).

Same point: The first parameter, this is the same, refers to the current object.

Different points, the second parameter is not the same, call is a list of parameters, apply is an array (arguments can also)

<script type= "Text/javascript" >functionPerson (name,age,love) { This. name=name;  This. age=Age ;  This. love=Love ;  This. say=functionsay () {alert ("Name:" +name); }      }      //Call mode    functionStudent (name,age) {Person.call ( This, Name,age); }      //Apply Method    functionTeacher (name,love) {person.apply ( This, [Name,love]); //person.apply (this,arguments);//With the same effect as the previous sentence, arguments    }      //the similarities and differences between Call and aplly:    //1, the first parameter, this is the same, refers to the current object    //2, the second parameter is different: Call is a list of parameters, and apply is an array (arguments can also)    varPer=NewPerson ("Wu Feng Lou", 25, "Wei-screen");//output: "Wu Feng Building"Per.say (); varstu=NewStudent ("mansion", 18);//output: "Mansion"Stu.say (); varTea=NewTeacher ("Narl", 16);//output: "Narl"Tea.say (); </script>

Iii. inheritance between object instances

Originally from: http://www.jb51.net/article/20431.htm

Three ways in which JavaScript implements inheritance

Related Article

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.