Function of arguments, call, apply

Source: Internet
Author: User

1.Arguments

Arguments.length is the number of function arguments, andArguments.callee refers to the function itself.

Arguments his features and how to use them

Characteristics:

Arguments objects and function are inseparable. Because the arguments object cannot be created explicitly, the arguments object is available only when the function is started.

How to use:

Although the arguments object is not an array, accessing a single parameter is the same way you access an array element

For example:

ARGUMENTS[0],ARGUMENTS[1],。。。。。。。。 Arguments[n],

In JS, you do not need to explicitly specify the parameter names, you can access them, for example:

function test () {  var s = "";  for (var i = 0; i < arguments.length; i++) {    alert (arguments[i]);    s + = Arguments[i] + ",";  }  return s;} Test ("Name", "age") output result: Name,age

We know that each object has its own properties, and the arguments object is no exception, first arguments access is like an array object, with 0 to arguments.length-1 to enumerate each element.

Let's take a look at the callee property, which returns the function object being executed, which is the body of the specified function object. The Callee property is a member of the arguments object and is available only if the related function is executing.
The initial value of the Callee property is the function object being executed, which allows anonymous recursive functions.

var sum = function (n) {  if (1 = = N) {    return 1;  } else {    return n + arguments.callee (n-1)}  } Alert (SUM (6)); 21 = 6 + 5 + 4 + 3 + 2 + 1

The popular point is thatarguments this object is used mostly for multiple calls to the same method and for different numbers of arguments. The method of execution is judged according to the index of the arguments.

2. Call

.

3.Apply

.

Function of arguments, call, apply

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.