About Apply and call

Source: Internet
Author: User

The role of 1.apply and call

--These two methods can hijack another object's method, inheriting another object's properties.

The difference and usage of 2.apply and call

——

function.apply (Obj,args)//The apply method has two parameters, the first is an object, and the second is an array. Function.call (obj,[param1[,param2[,... [, Paramn] ]])//Unlike the Apply method is the second starting argument, which is the argument list, and the second argument to apply is an array. Example://Applyfunctionpeople (name,age) { This. Name =name;  This. Age =Age ;}functionMan (name,age,sex) {people.apply ( This, arguments);  This. Sex = "man";}//Pagerfunctionpeople (age,name) { This. Name =name;  This. Age =Age ;}functionMan (name,age,sex) {People.call ( This, Age,name,sex);  This. Sex = "man";}

Note that in the example of apply, apply is used here because the order of the first two parameters of man and people is the same.

In the call example, since the first two parameter order of people and man is different, the parameter after this here using the Call,call method is where you can specify the corresponding value of the parameter list.

About Apply and call

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.