Call and apply (study notes)

Source: Internet
Author: User

Call Syntax:

function. Call (Thisarg, arg1, arg2, ...)

Parameters:

Thisarg: Optional. Indicates that this is modified to point to a color target. (Note: If the method is a function in a non-strict mode, then null and undefined will be replaced by the global object, and the original value will be converted to the object.) )

ARG1:,ARG2 is optional. Represents a list of arguments for a function that indicates which properties of the function inherit the past.

Example:

functionProduct (name, price) { This. Name =name;  This. Price =Price ;}functionFood (name, price) {Product.call ( This, name, price);  This. Category = ' food ';}functionToy (name, price) {Product.call ( This, name, price);  This. Category = ' toy ';}varCheese =NewFood (' feta ', 5);varFun =NewToy (' robot ', 40);

Other examples:

Use call on anonymous functions

 var  animals = [{species:  ' Lion ', name: ' King ' }, {species:  ' Whale ', Name: ' Fail ' }];  for  (var  i = 0; i < animals.length; I++ Span style= "COLOR: #0000ff" >function   (i) {  This . Print = function   () {Console.log (/ span> ' # ' + i + ' + this  .species + ': ' + this  .name);   this  .print (); }). Call (Animals[i], i);}  
anonymous functions use CALLD code

Use the Call function to specify the context for this

1 functiongreet () {2   varReply = [ This. person, ' was an Awesome ', This. Role].join (");3 Console.log (reply);4 }5 6 vari = {7Person: ' Douglas Crockford ', role: ' Javascript Developer '8 };9 TenGreet.call (i);//Douglas Crockford is an Awesome Javascript Developer
The Calling function specifies the context code for this

The calling function does not specify a parameter (the description of the parameter at the beginning of the reference text)

1 var sData = ' Wisen '; 2             3 function display () {4      This . SData); 5 }67//sData value is Wisen
do not specify parameters

The difference between call and apply is just. Call (This,arg1,arg2,...) is a list of parameters. Aoply (this,[arg1,arg2,...]) is a parameter array and is therefore no longer described.

Bind syntax:

Besides, dinner.

Call and apply (study notes)

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.