Callee (), call (), use of the Apply () method

Source: Internet
Author: User

argiments (class array object): A reference to an argument object.

Usage: When a function is called when the number of arguments passed in exceeds the number of formal parameters when the function is defined, there is no way to directly obtain an unnamed worthy reference, you can use arguments to access the argument value of the passed-in function by a numeric subscript

Arguments.callee: Refers to the function that is currently executing. Usage: The anonymous function calls itself recursively by callee.

var c=function (x) {
if (x<=1) {
Return 1
}
Return X*arguments.callee (x-1)
}

Arguments.length: Indicates the number of arguments passed into the function

Arguments.callee.length: Indicates the number of arguments expected to be passed in, that is, the number of formal parameters

The call () method and the Apply () method call Syntax:

Fun.call (thisarg[, arg1[, arg2[, ...])

Parameters

Thisarg

The This value that is specified when the fun function is run. Note that the specified this value is not necessarily the true this value when the function executes, and if the function is in non-strict mode, the this value, specified as null and undefined, is automatically directed to the global object (which is the Window object in the browser), and the value is the original value (number , String, Boolean) This will point to the automatic wrapper object for that original value.

Arg1, Arg2, ...

The specified argument list.

return value

The return result includes the specified this value and the parameter.

Example 1. Calling anonymous functions using the call method

var animals = [
{species: ' Lion ', Name: ' King '},
{species: ' Whale ', Name: ' Fail '}
];

for (var i = 0; i < animals.length; i++) {
(function (i) {
This.print = function () {
Console.log (' # ' + i + ' + ' + this.species + ': ' + this.name);
}
This.print ();
}). Call (Animals[i], i);
}

2. Calling the parent constructor using the call method

function Product (name, price) {
THIS.name = name;
This.price = Price;

if (Price < 0) {
Throw Rangeerror (' cannot create product ' +
THIS.name + ' with a negative price ');
}
}

function food (name, price) {
Product.call (this, name, price);
this.category = ' food ';
}


Apply syntax:

Fun.apply (thisarg[, Argsarray])

Parameters

Thisarg

The This value that is specified when the fun function is run. It should be noted that the specified this value is not necessarily the actual this value when the function executes, and if the function is in non-strict mode, it will automatically point to the global object when it is specified as null or undefined (the window object in the browser), and the value is the original value (number, String, Boolean) This will point to the original value of the automatic wrapper object.

Argsarray

An array or class array object in which the array elements are passed as separate arguments to the fun function. If the value of this parameter is null or undefined, it means that no parameters need to be passed in. Class array objects can be used starting from ECMAScript 5. For browser compatibility, see the bottom of this article.

The example uses Math.max/math.min to find the maximum/minimum value in an array.

var numbers = [5, 6, 2, 3, 7];

var max = Math.max.apply (null, numbers);
var min = Math.min.apply (null, numbers)

Callee (), call (), use of the Apply () method

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.