function CallThis point: Window return value: Determined by the return value, if there is no return statement, it means that there is no value returned
Case 1:function F1 () { console.log (this);} F1 (); Window case 2:function F2 () { return function () { console.log (this);} } var f2n = F2 (); f2n (); This-->window This is also a function call pattern Case 3:functioon F3 () { f3n (); The calling mode of the function--->window function f3n () { console.log (this);} } F3 ();
Method InvocationThis point: The caller return value of the method: determined by the return statement
Code Listing 1: var obj={ age:10, say:function () { console.log (this.age); } }; Obj.say ();//method call pattern Code 2: function F1 () { console.log (this); } var obj2={ s1:f1//Assigns the value of the F1 function to the OBJ2.S1 attribute }; OBJ2.S1 ();//method call pattern-->this:obj2 Code 3: var obj3={ age:10, s1:function () { Console.log ( this.age); } }; var obj4={ age:20, s2:obj3.s1//assigns the value of the OBJ3.S1 function to the S2 property }; OBJ4.S2 ();//method invocation mode: This-->obj4 function f5 () { } var a1=f5 ();//undefeind var a2=f5;// A2 is a function
Constructor Call This point: Object created by the current constructor return value : A, no manually added return value, default return this &N Bsp B, return a basic data type (number, Boolean, NULL, undefine D), final return this C, return a complex The data type (object) that eventually returns the object
Code Listing 1: function person () { this.age =; THIS.name = "Zhang San"; Console.log (this); } Constructor call pattern: var p1 = new Person ();//Print out the instance person {age:20, name: "Zhang San"} code 2; function P2 () { this.age = ; Return "ABC"; } var P2 = new P2 (); Console.log (p2); P2 {age:18} code 3: function P3 () { this.age = ten; return {}; } var p3 = new P3 (); Console.log (p3); Object {} Console.log (p3.age);//undefined Code 4: function P4 () { this.age = ten; return [1, 3, 5]; } var P4 = new P4 (); Console.log (p4);//[1, 3, 5] console.log (p4.age);//undefined
Context callsCall and apply are methods, which are methods that all functions have. Function.prototype
as long as the function calls the Call/apply method, the function executes immediately.
This points to: A, pass a null/undefined------------------->window B, pass a number, string, Boolean-------> corresponding basic wrapper class Type Object C, pass an object-------------------------------> point to the return value of the object: determined by the return statement
Function F1 () { console.log (this); } Context mode f1.call (null); Window f1.call (undefined); Window F1.call (1); Instance of number F1.call ("abc"); Instance of string F1.call (true); The Boolean instance var s1= "abc"; The code internally creates an instance of the string constructor that corresponds to the strings // {0: "A", 1: "B", 2: "C", length:3} //After the execution of the code, the instance is destroyed Console.log (s1.length); 3 Console.log (s1[0]); A
Four ways to invoke the JS type function