The call,apply of JavaScript is understood first

Source: Internet
Author: User

The call method invokes a function or method with a specified this value and a number of specified parameter values.

Note: The syntax for this function is almost identical to the syntax of the Apply () method, except that the Apply () method accepts a parameter array, and the call () method accepts a list of parameters.

Once we understand the concepts of these two methods, we will take a step at a pace to understand their application.


Change the direction of this within the method

Let's look at the following example first


var name = "Programmed person";

var age = 1;

var person = {

Name: "Public Number: Bianchengderen",

Age:20

}

function say () {

Console.log ("I Call:" +this.name+ ", Age:" +this.age)

}

Say ();//I am called: Programmed man, Age: 1

Say.call (person);//My name is: Public Number: Bianchengderen, Age: 20


The two calls are executed differently, and their results are different, unlike the Say method, which points to the various objects, the first execution points to the window, and we execute the call, which points to the person object in the Say method.

This is not a bit of pretending to be someone else's feeling. So what do we do? Of course, you can think more about what you can do! Let's go on down.


Implementing Inheritance Mechanisms

inheritance, which is a feature of advanced object-oriented, which we can use to call JavaScript.

Before looking at the example below, we must understand the above example well.


function person () {

THIS.name = "programmed person";

This.age = 20;

}

function Student () {

Person.call (this);

This.school = "Earth";

}

var student = new Student ();

Print it out below: Programmed man, 20, Earth

Console.log (Student.name,student.age,student.school);


In this example, the student function inherits the name and age attributes of the person, which is achieved by Person.call (this), and it should be understandable to understand the above example. So student has the character of person, and has his own personality , like this this.school.

Here, we do not involve the transfer of parameters to join, is convenient for everyone's understanding, need to add parameters to pass, you can hit the code to try, see how the effect!

Let's start with these two examples so that we don't understand the more confused. Everything needs to be simple to start, will be after the pursuit of a deeper side!


Also, remember to add application examples of apply!


This article belongs to Wu Shi Wei's blog, the public number: Bianchengderen original article, reproduced when please indicate the source and the corresponding link: http://www.wutongwei.com/front/infor_showone.tweb?id =158, welcome everyone to spread and share.


This article is from the "Ophir Technology Research" blog, please be sure to keep this source http://ophir.blog.51cto.com/1741965/1723481

The call,apply of JavaScript is understood first

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.