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 DeveloperThe 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)