'new' Operator Changes 'this' Inside A Function

by sikusikucom

JavaScript

function Person(name) {
    this.name = name;
    
    // true
    console.log(this.name === name);
}

function aPerson(name) {
    console.log(this);
}

// 'this' inside Person() refers to Person object.
var jim = new Person('Jim');

// 'this' inside aPerson() refers to 'window'. 
aPerson('Jill');

// 'this' inside aPerson() is assigned to aPerson object.
new aPerson('Jack');