JS in Call (), apply (), and the meaning of prototype

Source: Internet
Author: User

The most recent period of time to study the front-end, but some of the problems encountered I think it is necessary to dig into the

Prototype

1 JS with three methods of expression

class methods, attribute methods, prototype methods

functionpeople (name) { This. name=name; //Object Methods     This. Introduce=function() {Console.log ("My name is" + This. Name); }}//class Methodpeople.run=function() {Console.log ("I can Run");}//Prototyping MethodsPeople.prototype.introducechinese=function() {Console.log ("My name is" + This. name);}//Testvarp1=NewPeople ("XX");p 1. Introduce (); //My name is xxPeople.run ();//I can runP1. Introducechinese (); My name is XX.

In fact, from the above can be seen prototype, actually added a method to people, and this should be the official explanation "prototype property gives you the ability to add properties and methods to Objects"

2 Implementing Inheritance

functionBaseClass () { This. ShowMessage =function() {Console.log (' BaseClass: ', ' WOC)    }}//function Extendclass () {}functionExtendclass () { This. ShowMessage =function() {Console.log (' Extendclass: ', ' woc This is good body ')    }}functionExtendClass1 () {} Extendclass.prototype=NewBaseClass () Extendclass1.prototype=NewBaseClass ()varEC =NewExtendclass ()//Extendclass:woc This is good body

varEC1 =NewExtendClass1 ()//Baseclass:woc this was Bad boy
Ec.showmessage () Ec1.showmessage ()

It can be seen from the above case that if the showmessage of Extendclass () is present, it will point to itself, and if it does not exist, it will point to its "parent class".

Call () and Appyl ()

1 There is one prototype in each function, call (), apply ()

Call () apply () I simply understand to change the direction of your current object, which may be a bit abstract, look at the code

function method1 (arg1, arg2) {    return arg1+arg2}function  method2 (arg1, arg2) {     return arg1-arg2}var result1 = method2.apply (method1,[3,2]); var result2 = Method1.call (method2,3,3) Console.log (RESULT1);//1console.log (RESULT2);//6

From the above example, we can see that two function points have changed.

Call () apply (): This is the current this pointer that points to the function that called you (a bit like the meaning of copy)

The difference between the two is that apply () has only two parameters (the current method, the array) on the parameter,

The call () parameter is a single, single form

2 Implementing Inheritance

functionfather (word) { This. Word =Word This. showName1 =function() {Console.log (' Father say: ', This. Word)}}functionmother (word) { This. Word =Word This. showName2 =function() {Console.log (' mother say: ', This. Word)}}functionChild (word) {//father.apply (This,[word])Father.call ( This, Word) mother.call ( This, Word)}varc =NewChild (' Boys '); c.showname1 (); //Father Say:boysC.showname2 ();//Mother Say:boys

3 Good Cases

(1) Utilization

var result = Math.max (7.25,7.30)var array = [1,3,4,5,6,0,32.3,3.3]var RESULT1 = Math.max.apply (null, array); var result2 = Math.min.apply (null, array); Console.log (Result)  //7.3console.log (RESULT1) 32.3console.log (RESULT2)  //0

The parameter in JS Math.max () is not in the form of an array, and here is a clever way to do this by using apply (), first we don't need that object to point to Math, so we put a null as a parameter and then pass the arary array into it.

(2) Understanding

functionBaseClass () { This. showmsg =function() {Console.log ("Baseclass::showmsg"); }     This. baseshowmsg =function() {Console.log ("Baseclass::baseshowmsg"); }}baseclass.showmsg=function() {Console.log ("Baseclass::showmsg Static");}functionExtendclass () { This. showmsg =function() {Console.log ("Extendclass::showmsg"); }}extendclass.showmsg=function() {Console.log ("Extendclass::showmsg Static")}extendclass.prototype=NewBaseClass ();varInstance =NewExtendclass (); instance.showmsg ();//Show Extendclass::showmsgInstance.baseshowmsg ();//Show Baseclass::baseshowmsgInstance.showmsg ();//Show Extendclass::showmsgBaseClass.showMsg.call (instance);//Show baseclass::showmsg StaticvarBaseinstance =NewBaseClass (); Baseinstance.showMsg.call (instance);//Show Baseclass::showmsg
View Code

JS in Call (), apply (), and the meaning of 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.