call (), apply () arguments array object class and method

. 1  <! DOCTYPE HTML > 
2  < HTML > 
. 3      < head > 
. 4          < Meta charset = "UTF-. 8" > 
. 5          < title > </ title > 
. 6      < Script type = "text / JavaScript" > 
. 7     / * 
. 8     Call ( ), apply ()
 . 9     method both methods are function object, the object needs to be called through the function
 10     when performing a function call to call () and apply () will call the function
 11     call call () and apply () can the object as a first parameter
 12       at this time will become the object of this function is executed when
 13     call () method arguments may be sequentially transmitted after the object
 14      Apply () method requires the argument is encapsulated into a unified transfer array
 15     * / 
16    function Fun (A, B) {
 . 17        the console.log ( " A = " + a);
 18 is        the console.log ( " B = " + B);
 . 19        Alert ( the this .name); // the this is a call, the first call to apply the parameter 
20 is    }
 21 is    var obj = {name: " WXY " };
 22 is    var obj2 = {name: " 111 "};
 23 is    // fun.call (obj); // WXY 
24    // fun.call (obj2); 111 // 
25    fun.call (obj, 2 , . 3 );
 26 is    fun.apply (obj, [ 2 , 3 ]);
 27    / * 
28    call the function, the browser every time passing in two parameters implied,
 29    context object 1. function the this
 30    objects 2. packages argument arguments
 31 is      arguments array is a class object, it is also possible to manipulate the data via an index, may acquire the length of the actual parameter
 32      when calling the function, we pass arguments are encapsulated arguments in
 33      even if we do not define the shape parameter may be used by arguments solid reference
 34 is      arguments [0] indicates the first argument
 35      arguments [. 1] indicates the second argument
 36     arguments have a property called the callee
 37 [      This property is a function corresponding to the object, the object is currently pointing function
 38 is    * / 
39   
40   function fun2 () {
 41 is       the console.log (The arguments.length); // 2 
42 is       the console.log ( arguments [ 0 ]); // 12 is 
43 is  }
 44 is  fun2 ( 12 is , . 3 );
 45      </ Script > 
46 is      </ head > 
47      < body > 
48      </ body > 
49  </ HTML >

 

Guess you like

Origin www.cnblogs.com/zuiaimiusi/p/11225176.html