Khi gọi new MyFunc(), JS thực hiện 4 bước: 1) Tạo object rỗng mới, 2) Gán MyFunc.prototype làm prototype của object đó, 3) Chạy constructor với this là object mới, 4) Tự động return this — trừ khi constructor return tường minh một object khác.
Ví dụ: function Person(name) { this.name = name; } const p = new Person('An') tạo object {name: 'An'} với Person.prototype trong prototype chain. Bẫy: quên new sẽ khiến this trỏ vào global object (hoặc undefined ở strict mode), gây bug khó tìm.
When new MyFunc() is called, JS performs 4 steps: 1) Create a new empty object, 2) Set MyFunc.prototype as the object's prototype, 3) Run the constructor with this as the new object, 4) Automatically return this — unless the constructor explicitly returns another object.
Example: function Person(name) { this.name = name; } const p = new Person('An') creates {name: 'An'} with Person.prototype in the prototype chain. Trap: forgetting new causes this to point to the global object (or undefined in strict mode), causing hard-to-find bugs.