the point to this in the constructor
related codes
I"ve been reading js advanced programming recently, which is a little confusing.
in ordinary functions, this points to global scope:
var name = "moon";
function sayName () {
var name = "star";
console.log(this.name); //moon
}
but in the constructor, who is this pointing to:
function Person (name, age) {
this.name = name; //
this.age = age;
}
var p1 = new Person ("sun", 22);
book says: assign the scope of the constructor to the new object, so this points to the new object. Why the first case this is global scope, while the second case is functional scope? solve the puzzle