About js point of this constructor

First come appetizer:

function person(name) {
            // variable scope for the internal function, external inaccessible, preventing pollution and variable name conflicts
            var name = 'Bob';
            this.name = name;
            this.sayName = function() {
                console.log(name);
            }
            this.changeName = function(newName) {
                name = newName;
            }
        }
        // external variables can not access the internal
        var a = new person();
        console.log(a.name)
Running about

First of all there are no JS class.
A constructor is a function, this points to the object, this also means that less than blindfolded refers to the constructor.
Instance object constructor this point to create doubt. To understand this, we must first understand , call the constructor with the new operator at all times what is happening.
I can just say the answer is a constructor, as they moved me here:

In fact, constructors and the normal function is essentially no difference, the only difference between the two:

Function capitalized, this distinction is only convention, easy to distinguish. You really want to lowercase defined constructor also no problem, so the difference is negligible.

Call the constructor of the need to use the new operator, while calling ordinary functions and many different kinds, but never use the new operator. So, the difference between the constructor and general functions in the new operator, let us now look carefully at the new operator.
With what happens when the new operator to create an object identifier:

** Step 1: Create an Object object instance.
Step two: performing object constructor assigns the newly generated instance.
The third step: execution code constructor
fourth step: Returns the newly generated object instance **

Note: The original constructor window object methods, if not directly invoke the new operator, the execution object constructor is the window, that is, this points to the window. Now after the new operator, this points to the newly created object. This step is crucial to understand.
Constructor code execution, look at the code:

function Person(){
this.name = "Tiny Colder";
var age = 22;
window.age = 22;
}
var p = new Person();
alert(p.name)//Tiny Colder;
alert(p.age)//undefined;
alert(window.age)//22;

When you create an object with the new operator, first you create an object instance, and then execute the code. So still entangled, when a constructor defined attributes will be inherited by instances of an object, it can look so:

var p = new Object();
p.name = "Tiny Colder";

It is common to create objects, methods, properties and then add to the object. If each create an object, you need a few lines of code, it is undoubtedly the worst. This needs to happen with this correspondence: new operator, automatically executed constructor code. So we can save when you add duplicate attribute redundant code. How to add new objects generated in it when these properties?

The second step already said: the execution object constructor assigned to a new generation of this instance. Combined in the preceding paragraph to say, in the constructor is executed automatically this.name = "Tiny Colder"; when the equivalent of performing p.name = "Tiny Colder"; and the constructor
var age = 22; statement, It will perform but does not affect the newly created object. window.age = 22; statements will be executed and will add a window object attributes. alert as evidence.

Perhaps here, you understand the first three steps of the new operator, it is important three steps. But this function is how to return the object of it? We did not see any statement related with the return. This is the final step in the new operator: Returns the newly generated.
If the function is called without an explicit return expression (only return the object), the implicit return this object - that is, the newly created object.

Now look at this Code:

function Person(){
this.name = "Tiny Colder";
return {};
}
var p = new Person();
alert(p.name)//undefined;

An object is created so out.
In fact,

var p = new Person();

var p = new Object();
Person.apply(p);

The effect is the same.

 

Brief summary:

An explicit return the following values: undefined, null, boolean, number and other basic type, and will not replace the default behavior of new-style calls.

However, an explicit return the following values: {}, [], RegExp, Date, Function, new calls will replace the default return value this.

We have seen, the latter, all objects are complex types.

 

If the body of the function is strict mode, it will not bind to this global object, such as:

var  a =  function  (){ 'use strict' ; this .b =  'b' return  /111/g};
a();  // 直接报错
Because strict mode, the default this point undefined

Guess you like

Origin www.cnblogs.com/jayfeng/p/12173439.html