On function and arguments_javascript skills in JavaScript

Source: Internet
Author: User
Tags function definition function prototype

function of JavaScript

Property:

1, Arguments Objects

2, caller

A reference to a function that calls a single preceding function, if it is called by the top-level code,
Returns null (Firefox returns undefined).
Note: It makes sense only when code executes

3, length

Declaring a function is the number of named arguments specified (function definition is, the number of parameters defined)

4, prototype

An object that is used to construct a function that defines the properties and methods of the
shared by all objects created by the constructor.

Method:

Applay ()--> applay (this,[])
Call ()--> call (this, variable parameter)
ToString ()

Arguments object for JavaScript

The arguments object is defined in the function body only, and he is an array of classes (an object is not an array, but some attributes of an array).

Description

When a function is called, a arguments object is created for the function.
The local variable arguments automatically initializes and references the arguments object (arguments is a reference to the arguments object)

The properties of this object:

1, callee

A reference to the function that is currently executing

2, length

The number of arguments passed to the function (actually passed to the number of function arguments)

Arguments characteristics

The arguments object cannot be explicitly created, and the arguments object is available only when the function begins. the arguments object of a function is not an array and accesses a single argument in the same way that it accesses an array element. Index n is actually one of the parameters of the 0...N property of the arguments object.

In JavaScript, you do not need to explicitly indicate the parameter names to access them. Such as:

function Hi () {
if (arguments[0]== "Andy") {return
   ;
}
Alert (arguments[0]);

Length property of arguments

Meaning

Returns the number of actual arguments that the caller passed to the function.

Usage

[function.] Arguments.length

The option function parameter is the name of the function object that is currently executing.

Description

When a function object starts executing, the script engine initializes the length property of the arguments object to the number of actual arguments passed to the function.

JS will not take the initiative for you to determine how many parameters you have passed to the function, if you pass more, the spare part is not used, if you less pass, then the parameter value is undefined

So we can use the arguments length property to detect if the correct number of actual arguments are used when calling a function, because JavaScript doesn't do it for you.

Arguments's 0...N properties

Meaning

Returns the actual value of each parameter in a arguments object, which is returned by the arguments property of a function that is executing.

Usage

[function.] arguments[[0|1|2|...| N]]

Parameters

function

Options available. The name of the Function object that is currently executing.

0, 1, 2, ..., n

Required option. A non-negative integer in the range 0 through N, where 0 represents the first argument and N represents the last argument. The value of the last parameter n is arguments.length-1

Description

0. The value returned by the. N property is the actual value passed to the function being executed. Although it is not actually an array of parameters, you can access the parameters that make up the arguments object in the same way that you access the array elements.

Example

The following example demonstrates the use of the 0 ... n attribute of the arguments object.

function Argtest () {
  var s = "";
  S + + "The individual arguments are:" For
  (n=0; n< arguments.length; n++) {
   s + = argtest.arguments[n];
   s + = "";
  }
  return (s);
}
Print (Argtest (1, 2, "Hello", New Date ());

callee Properties of Arguments

Meaning

Represents a reference to the function object itself, which is the body of the specified function object, which facilitates the recursive implementation of the Nameless function or the encapsulation of the function.

Usage

[function.] Arguments.callee

The optional function parameter is the name of the function object that is currently executing.

Description
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 that is being executed. This allows anonymous recursive functions.

Instance:

Calculates the sum of the natural numbers of 1 to n by recursion:

<script>
 
  var sum=function (n) {  if (1==n) {return
    1;  } else {return
    n +  Arguments.callee (n-1);
   }
alert (sum);
</script>  

A description of the functional function prototype prototype:

When an object is initialized by a constructor,

The New keyword initializes the object by calling the constructor and passes the object as the value of the This keyword.

At the same time, the New keyword also sets the prototype of this object, the prototype of which is the value of the prototype property of its constructor.

(for example: a = new Date (), the prototype of object A is Date.prototype)

All functions have a prototype property, and when the function is "defined", the prototype property is automatically created and initialized.

The initialization value of the prototype property is an object, and the object has only one property, and this property is constructor,

It refers back to the constructor associated with the prototype.

This article on the JavaScript function and arguments is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

Related Article

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.