The constructor can be used with new, representing the creation of an object, or as a normal function call because it is also a function.
function person (name) {this.name=name;} Person (n); alert (window.name);//12
You can see that this represents a global window object when the constructor is called as a normal function. Obviously it is not a good practice to call a constructor function as a normal function, and there is no reason to do so. In practice, such strange usages should be eliminated in order not to produce strange problems.
function Person (name,sex) { this.name=name; var name1= "a"; Name2=sex;} var per = new Person ("Aty", "Boy"), alert (per.name),//Atyalert (PER.NAME1);//undefinedalert (per.name2);// Undefinedalert (window.name2);//boy
The constructor defines the variable using this is a member variable; var is a local variable, and no keyword is a global variable that is added to the Window object. This explains why the This keyword is used in constructors.
Scope problems of variables in JavaScript constructors