Javascript arguments,calle,caller,call,apply

Source: Internet
Author: User
Tags access properties
<span id="Label3"> </p><pre class="brush:javascript;gutter:true;">one, arguments The object represents the function being executed and the arguments that call his Function. [function.] arguments[n] parameter Function: Option. The name of the Function object that is currently Executing. N: Option. The 0-based parameter value index to pass to the Function Object. Description: arguments is a hidden object created in addition to the specified arguments when a function call is Made. Arguments is an array-like but not an array object, saying that he resembles an array because it has the same access properties and methods as the array, can be accessed by arguments[n] to the value of the corresponding individual parameter, and has the length property of the Array. also, the arguments object stores the arguments that are actually passed to the function, not limited to the list of arguments defined by the function declaration, and the arguments object cannot be created Explicitly. Here you add a code that illustrates that arguments is not an array class: Array.prototype.selfvalue = 1;alert (new array (). selfvalue); function testaguments () {alert (arguments.selfvalue);} Run the code and you will see that the first alert shows 1, which means that the array object has the Selfvalue property, the value is 1, and when you call the function testaguments, you will find that the " Undefined ", describes a property that is not arguments, that is, arguments is not an array object. second, Caller returns a reference to the function that called the current Function. The Functionname.caller functionname object is the name of the function being Executed. Description: for a function, the caller property is defined only when the function Executes. If the function is called by the top level, then caller contains Null. If you use the Caller property in a string context, the result is the same as functionname.tostring, which means that the inverse of the function is Displayed. The following example illustrates the use of the caller property: function Callerdemo () {if (callerdemo.caller) {var a= callerDemo.caller.toString (); Ale RT (a);} Else{alert ("this is a top function");}}function Handlecaller () {callerdemo ();} Callee returns the function object being executed, which is the body of the specified function Object. [function.] The Arguments.callee optional function parameter is the name of the function object that is currently Executing. Description: the initial value of the Callee property is the Function object that is being Executed. The Callee property is a member of the arguments object that represents a reference to the function object itself, which facilitates the recursion of anonymous functions or ensures the encapsulation of functions, such as the recursion of the following example calculates the sum of 1 to N of the natural number. This property is available only if the related function is Executing. It is more important to note that callee has the length property, which is sometimes used for validation or better. Arguments.length is the actual parameter, and the arguments.callee.length is the length of the formal parameter, which can determine whether the parameter length is consistent with the argument Length. Callee can print its own function Calleedemo () {alert (arguments.callee);} Recursive calculation var sum = function (n) {if (n <= 0) return 1; else return n +arguments.callee (n-1)} more general recursive functions: var sum = function (n) {if (1==n) return 1; else return n + sum (n-1);} Call: Alert (sum (100)), where the function contains a reference to the sum itself, the function name is just a variable name, call sum inside the function is equivalent to calling a global variable, does not well reflect the call itself, when using callee will be a better method. Iv. apply and call their role is to bind the function to another object to run, the two are only defined in the way the parameter is distinguished: apply (thisarg,argarray); Call (thisarg[,arg1,arg2 ...]); That is, the this pointer inside any function is assigned a value of thisarg, which allows for the purpose of running the function as a method of another object description of Apply: if Argarray is not a valid array or is not a arguments object, it will result in a TypeerrOr If no parameter is provided for Argarray and thisarg, then the Global object will be used as a thisarg and cannot be passed any Parameters. Description of Call: The call method can change the object context of a function from the initial context to the new object specified by Thisarg. If the Thisarg parameter is not provided, then the global object is used as a thisarg-related tip: applying call and apply has a trick in it, which is to apply another function (class) with call and apply, The current function (class) has the method or property of another function (class), which can also be called "inheritance". Look at the following example://inherited demo function base () {this.member = "dnnsun_member"; This.method = function () {window.alert (this.member); }}function Extend () {base.call (this); Window.alert (member); Window.alert (this.method);} The above example shows that, after call, extend can inherit the methods and properties of Base. By the way, using apply in the JavaScript framework prototype to create a schema that defines the class with the following implementation code: var class = {create:function () {return function () {t His.initialize.apply (this, arguments); }}}: from the code view, the object contains only one method: Create, which returns a function, the class. But this is also the constructor of the class, where initialize is called, and this method is the initialization function defined at the time the class is Created. In this way, you can implement the class creation pattern in prototype</pre><p><p>  </p></p><p><p>Javascript arguments,calle,caller,call,apply</p></p></span>

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.