JS call method apply method caller attribute callee attribute

Source: Internet
Author: User
JS call method _ apply method _ caller attribute _ callee attribute

   

Address: http://aweber.blogbus.com/logs/46751586.html

I. Call Method
Call a method of an object to replace the current object with another object (in fact, it is to change the internal pointer of the object, that is, to change the content pointed to by this object ).

JS Code
  1. Call ([thisobj [, arg1 [,Arg2 [,  [,. Argn])

Parameters
Thisobj
Optional. Will be used as the object of the current object.
Arg1, arg2, , Argn
Optional. The method parameter sequence will be passed.
Description
The call method can be used to call a method instead of another object. The call method can change the object context of a function from the initial context to the new object specified by thisobj. If the thisobj parameter is not provided, the global object is used as thisobj.

JS Code
  1. <InputType ="Text" id = "mytext" value = "InputText">
  2. <SCRIPT>
  3. Functionobj () {This. value = "Object !";}
  4. Varvalue = "GlobalVariable";
  5. Functionfun1 () {alert (this. Value );}
  6. Window. fun1 ();  // GlobalVariable
  7. Fun1.call (window ); // GlobalVariable
  8. Fun1.call (document. getelementbyidx_x_x ('mytext'); // InputText
  9. Fun1.call (New OBJ (); // object!
  10. </SCRIPT>
JS Code
  1. Varfirst_object={
  2. Num:42
  3. };
  4. Varsecond_object={
  5. Num:24
  6. };
  7. Functionmultiply (mult){
  8. Returnthis. Num*Mult;
  9. }
  10. Multiply. Call (first_object,5 );//Returns42*5
  11. Multiply. Call (second_object,5 );//Returns24*5

Ii. Apply Method
The first parameter of the apply method is the object to be passed into the current object, that is, this in the function. The following parameters are the parameters passed to the current object.
The apply and call functions are the same, but they have different parameters. The meaning of the first parameter is the same, but for the second parameter: Apply is a parameter array, that is, multiple parameters are combined into an array and passed in, call is passed in as the call parameter (starting with the second parameter ).
For example, func. the apply method for call (func1, var1, var2, var3) is: func. the benefit of applying (func1, [var1, var2, var3]) is that the arguments object of the current function can be directly passed as the second parameter of apply.

JS Code
  1. VaR func = newfunction () {This. A = "func "}
  2. Varmyfunc = function (x, y ){
  3. Vara = "myfunc ";
  4. Alert (this. );
  5. Alert (x+Y );
  6. }
  7. Myfunc. Call (func, "var","Fun");//"Func""VaRFun"
  8. Myfunc. Apply (func, ["Var ","Fun"]); //"Func""VaRFun"

Iii. Caller attributes
Returns a reference to the function, that is, the function body that calls the current function.
Functionname. Caller: the name of the function to be executed.
Note:
For a function, the caller attribute is defined only when the function is executed. If the function is called by the top layer of the JScript program, caller contains null. If the caller attribute is used in the string context, the result is the same as functionname. tostring, that is, the decompilation Text of the function is displayed.

JS Code
  1. <SCRIPT>
  2. Functioncalllevel (){
  3. If (calllevel. Caller=Null)
  4. Alert ("calllevelWasCalledFromTheTopLevel .");
  5. Else
  6. Alert ("calllevelWasCalledByAnotherFunction: \ n"+ Calllevel. Caller );
  7. }
  8. Functionfuncaller (){
  9. Calllevel ();
  10. }
  11. Calllevel ();
  12. Funcaller ()
  13. </SCRIPT>

Iv. callee attributes
Returns the function object being executed, that is, the body of the specified function object.
[Function.] arguments. callee: the name of the currently executed function object.
Note:
The initial value of the callee attribute is the function object being executed.
The callee attribute is a member of the arguments object. It indicates a reference to the function object.
Recursion of a function or encapsulation of a function. For example, the following example recursively calculates the sum of natural numbers from 1 to n. This attribute
It is available only when the related function is being executed. Note that callee has the Length attribute, which is sometimes
It is better for verification. Arguments. length is the length of the real parameter, and arguments. callee. length is
The length of the parameter to determine whether the length of the parameter is consistent with that of the actual parameter.

JS Code
  1. // Callee can print itself
  2. Functioncalleedemo (){
  3. Alert (arguments. callee );
  4. }
  5. // Used to verify Parameters
  6. Functioncalleelengthdemo (arg1,Arg2){
  7. If (arguments. Length = arguments. callee. length){
  8. Window. Alert ("verifying the length of the form parameter and real parameter is correct !");
  9. Return;
  10. }Else {
  11. Alert ("real parameter length:" + arguments. Length );
  12. Alert ("parameter length:"+ Arguments. callee. Length );
  13. }
  14. }
  15. // Recursive Calculation
  16. VaR sum=Function (n ){
  17. If (n<=0)
  18. Return 1;
  19. Else
  20. Returnn+ Arguments. callee (n-1)
  21. }

V. Bind

JS Code
  1. <ScriptType ="Text/JavaScript">
  2. Varfirst_object={
  3. Num:42
  4. };
  5. Varsecond_object={
  6. Num:24
  7. };
  8. Functionmultiply (mult){
  9. Returnthis. Num*Mult;
  10. }
  11. Function. Prototype. Bind=Function (OBJ){
  12. VaR method=This,
  13. Temp=Function (){
  14. Returnmethod. Apply (OBJ,Arguments );
  15. };
  16. Return temp;
  17. }
  18. Varfirst_multiply=Multiply. BIND (first_object );
  19. First_multiply (5 );//Returns42*5
  20. Varsecond_multiply=Multiply. BIND (second_object );
  21. Second_multiply (5 );//Returns24*5
  22. </SCRIPT>

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.