JQuery pagenation Knowledge Point finishing--arguments,callee,caller,apply application (20150517)

Source: Internet
Author: User
Tags access properties

Arguments

The object represents the function being executed and the parameters of the function that called it.

[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 that is 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 it resembles an array because it has an array of access properties and methods, which can be accessed by arguments[n] to access the value of the corresponding single 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. The arguments object is available only when the function starts.

123<body>4<script type= "Text/javascript" >5 //<! [Cdata[6 functionArgtest (A, b) {7    varI, S = "The Argtest function expected";8    varNumargs = Arguments.length;arguments.length is the number of arguments that are taken when the function is called//gets the numeric value of the passed parameter. Argtest (' A ', ' B '): numargs=2, expargs=29    varExpargs = Argtest.length;; Argtest.length the number of arguments specified for the function//gets the numeric value of the desired parameter. Argtest (' A ', ' B ', ' C '): numargs=3, expargs=2Ten    if(Expargs < 2) Ones + = Expargs + "argument."; A    Else -s + = Expargs + "arguments."; -    if(Numargs < 2) thes + = Numargs + "was passed."; -    Else -s + = Numargs + "were passed."; -s + = "/n/n" +     for(I =0; i < Numargs; i++) {//gets the contents of the parameter.  -s + = "Arg" + i + "=" + Arguments[i] + "/n";; Arguments similar arrays can be obtained by arguments[] for each parameter value +    } A    return(s);//returns a list of parameters.  at } -  -Window.document.write (argtest (' AAA ', ' bbbb ', ' cccc ', ' dddd '))); - //]]> -</script>

Caller [Summoner]

Returns a reference to the function that called the current function.
Functionname.caller
The 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, that is, the deserialized text of the function is displayed.
The following example illustrates the use of the caller property://Caller Demo {

1 functionCallerdemo () {2     if(callerdemo.caller) {3         varA=callerDemo.caller.toString ();4 alert (a);5}Else {6Alert ("This is a top function");7       }8 }9 functionHandlecaller () {Ten Callerdemo (); One}

Handlecaller (), returns function Handlecaller () {Callerdemo ();} "That is, Callerdemo ( ) returns the string form of the function itself that uses it," and if you run Callerdemo () directly, return function Anonymous () {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. Indicates that the initial value of the Callee property is the Function object being executed. The Callee property is a member of the arguments object that represents a reference to the function object itself, which facilitates anonymous
recursive function or guarantee the encapsulation of functions, such as the following example of the recursive calculation of 1 to n the sum of natural numbers. And this property
Available only if the related function is executing. It is also important to note that Callee has the length property, which is sometimes
For validation or better. Arguments.length is the argument length, Arguments.callee.length is
Parameter length, which can be used to determine if the parameter length is the same as the argument length at the time of the call. Example//callee can print its own

1 functionCalleedemo () {2 alert (Arguments.callee);3 }4 //Calleedemo () returns the Calleedemo () function itself as a string: function Calleedemo () {alert (Arguments.callee);}5 //used to validate parameters6 functionCalleelengthdemo (arg1, arg2) {7     if(arguments.length==arguments.callee.length) {8Window.alert ("Verify that the parameters and argument lengths are correct!") ");9         return;Ten}Else { OneAlert ("Argument length:" +arguments.length);//the actual parameter with the Calleelengthdemo (arg1, arg2) function is used: Calleelengthdemo (' a ') argument is 1 AAlert ("Parameter length:" +arguments.callee.length);//number of parameters defined by the Calleelengthdemo (arg1, arg2) function: 2 -       } - } the //Recursive calculation - varsum =function(n) { -   if(n <= 0)                         -   return1; +   Else -     returnn + arguments.callee (n-1)//Arguments.callee invokes the currently executing function, which is the sum itself!  +More general recursive functions:varsum =function(n) { A     if(1==n)return1; at     Else returnn + sum (n-1); -}

When invoked: Alert (SUM (100));
Where the function contains a reference to the sum itself, the function name is just a variable name, and calling sum inside the function is equivalent to calling
A global variable, not a good representation of the call itself, when using callee will be a better method.
Callee can print itself

Call and apply

Their role is to bind the function to another object to run, and the two differ only in the way that the parameter is defined: Apply (Thisarg,argarray); Call (Thisarg[,arg1,arg2 ...]);
That is, the this pointer inside all functions is assigned a value of Thisarg, which enables the purpose of running a 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 none of the Argarray and Thisarg parameters are provided, then the Global object will be used as a thisarg,
and cannot be passed any parameters.
the description of 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 also has a technique in which another function (class) is applied with call and apply, and the current
A function (class) has a method or property of another function (class), which can also be called "Inheritance". Look at the following example://Inherited Demo

1 functionBase () {2      This. member = "Dnnsun_member";3      This. method =function() {4Window.alert ( This. Member);5       }6 }7 functionExtend () {8Base.call ( This);9 Window.alert (member);TenWindow.alert ( This. method); One}


After calling extend (), Window.alert (member) = "Dnnsun_member;window.alert (this.method) =" function () {Window.alert ( This.menber);}

As can be seen from the above example, extend can inherit the methods and properties of base after call. By the way, using apply in the JavaScript framework prototype creates a schema that defines the class, with the following implementation code:

1 var Class = {2     function() {3     returnfunction () {4       this. initialize.apply (this, arguments); 5       }6    }7 }

Parsing: From the code perspective, the object contains only one method: Create, which returns a function, the class. But it's also a class.
constructor, which calls initialize, which is the initialization function defined at the time the class is created. Through this way,
You can implement the class creation pattern example in prototype: Var vehicle=class.create ();

1Vehicle.prototype={2Initializefunction(type) {3          This. type=type;4       }5Showself:function(){6Alert ("This vehicle is" + This. type);7       }8}varmoto=NewVehicle ("Moto");9Moto.showself ();

Author: Sjolzy| Google + Address:Http://sjolzy.cn/Understanding-JavaScript-in-argumentscalleecallerapply.html

--eof--

JQuery pagenation Knowledge Point finishing--arguments,callee,caller,apply application (20150517) (EXT)

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.