In the most recent study on this issue pointed to himself to write essays, make a summary
1. this function in the general
The reason is simple, the function is not called, is not executed, the largest object in the window is the window, directly fn () function call is equivalent to window.fn (), all this points to window
2. this assignment in the function
var Fn = function () {
the console.log ( the this ); // output result window } fn()
This is not connected with the equal sign fn it, why not function inside this point fn? In fact, the above is the same question, who calls who directed, although the function is assigned to a variable, but if you do not print directly fn brackets, parentheses output is the content of an entire function, and does not point to a direct print this, indicating that function at run time or to call, since it is called in global state, of course, is the implementation of this window
3. this time the constructor
function Star(uname) { this.uname = uname; console.log(this); } Star (); // this fact does not make sense, because the constructor itself is a target template var jsnow = new new Star ( "Snow in June" ); console.log (jsnow) // Star {uname: "Serissa"}
Output, point objects are instantiated
In the method 4.this time the constructor, this point
function Star(uname) { this.uname = uname; this.sing = function () { the console.log ( the this ); // output Star {uname: "Serissa", Sing: ƒ} } } var jsnow = new new Star ( "Liuyuexue" ); jsnow.sing()
Constructor this point, still objects instantiated
5. In the prototype method, when the plug
function Fn(uname) { this.uname = uname; } Fn.prototype.sing = function () { the console.log ( the this ); // output Fn {uname: "Serissa"} } var jsnow = new new the Fn ( "Serissa" ) jsnow.sing()
this refers to an object to be instantiated
6. When using the prototype method for a plug when the object
function Fn(uname) { this.uname = uname; } Fn.prototype = { sing: function () { console.log(this); }, dance: function () { console.log("just so so") } } var jsnow = new new the Fn ( "Serissa" ) jsnow.sing () // output still is instantiated objects jsnow.dance ()
7. When the function returns with a return value and the value is a simple data types
var Fn = function () { this.age = 18; return 1; } var jsnow = new Fn (); console.log (jsnow.age); // Output 18
Great God around here explained that the return value is a simple data type when in fact there is no and there is no difference, it does not affect the output results
8. When the return value of complex data types when (I was learning phase), that function distance
var Fn = function () { this.age = 18; return null; } var jsnow = new Fn (); console.log(jsnow.age); //18
The return value actually became 18 ... (special memories chant)
As for why, I can not explain, probably because null is a special object of it, to be honest, I'm puzzled why the null when the design would be a target, of course, some things as a conclusion in mind on the line, do not have to pursue Why; otherwise, I would like to ask why the Chinese people to use English to write code
When the return value of complex data type, returns a value underfind time, it means that this point has changed, no longer points to the object is instantiated, but the complex data type returned
**********
Of course, when it comes to objects, had thought of a special object, null, print typeof time, null return value is actually an object
console.log(typeof null) //object
Then return null when the object try