JavaScript's function and its Arguments

Source: Internet
Author: User
Tags function prototype

http://shengren-wang.iteye.com/blog/1343256

function of JavaScript
Property:
1. Arguments Object
2, caller
A reference to the function that invokes the pre-order functions, if it is a top-level code call,
Returns null (Firefox returns undefined).
Note: Only makes sense when code executes
3. Length
Declaring a function is the number of named arguments specified (function definition is, number of parameters defined)
4, prototype
An object that is used for constructors, properties and methods defined by this object
All objects that are created by the constructor are shared.
Method:
Applay ()--Applay (this,[])
Call ()--Call (this, variable parameter)
ToString ()


JavaScript's arguments Object
The arguments object is defined only in the body of a function, which is an array of classes (objects are not arrays, just have some characteristics of the array).
Description
When a function is called, a arguments object is created for the function,
The local variable arguments automatically initializes and references that arguments object (arguments is a reference to the arguments object)

The properties of the object:
1, callee
A reference to the currently executing function
2. Length
The number of arguments passed to the function (the number of arguments actually passed to the function)

Http://www.studyofnet.com/news/215.html

Arguments characteristics

Arguments objects cannot be created explicitly, arguments objects are available only when the function is started. the arguments object of a function is not an array, and a single parameter is accessed in the same way as 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 specify the parameter names to access them. Such as:

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

the Length property of the arguments

Meaning

Returns the number of actual arguments passed to the function by the calling program.

Usage

[function.]arguments.length

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

Description

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

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


So we can use the length property of arguments to detect if the function is called using the correct number of actual arguments, because JavaScript doesn't do it for you.


0...N properties of arguments

含义

返回一个 arguments 对象中的各个参数的实际值,相应的值是由一个正在执行的函数的 arguments属性返回的。

用法

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

参数

function

可选项。当前正在执行的 Function 对象的名称。

0, 1, 2, …, n

必选项。0 到 n 范围内的非负整数,其中 0 代表第一个参数而 n 代表最后一个参数。最后参数 n 的值为 arguments.length-1

说明

0 . . . n 属性所返回的值就是传递给正在执行的函数的实际值。尽管实际上并不是一个参数数组,您还是可以按照与访问数组元素的方法相同的方式访问组成 arguments 对象的各个参数。

示例

下面的例子演示了 arguments 对象的 0 . . . n 属性的用法

 
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()));

The callee property of arguments

Meaning

Represents a reference to the function object itself, which is the body of the specified function object, which facilitates recursion of the nameless function or guarantees the encapsulation of the function.

用法

[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 for anonymous recursive functions.

Instance:

Calculates the sum of natural numbers from 1 to n using recursion:

<script>
   var sum=function (n){   {       return 1;   {       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 it as the value of the This keyword.
At the same time, the new keyword sets the prototype of this object, and the prototype of an object is the value of the prototype property of its constructor.
(Example: a = new Date (), the prototype of a object 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 this object has only one property, and this property is constructor,
It refers back to the constructor associated with the prototype.

JavaScript's function and its Arguments

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.