Khi dùng new với constructor có hàm nội bộ, this bên trong hàm nội bộ không trỏ đến instance. Khắc phục bằng cách gán this vào biến khác (ví dụ var currentTechnologyName = this).
Mã thực hiện
function Employee() {
this.technologyName = "JavaScript";
var currentTechnologyName = this;
function workingTechnology() {
console.log("I am working with " + currentTechnologyName.technologyName + " Technology");
}
workingTechnology();
}
var currentTechnology = new Employee();
Kết quả
I am working with JavaScript Technology