Từ khóa
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 của hàm hoạt động hơi khác một chút trong JavaScript so với các ngôn ngữ khác. Nó cũng có một số khác biệt giữa chế độ nghiêm ngặt và chế độ không nghiêm ngặtTrong hầu hết các trường hợp, giá trị của
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 được xác định bằng cách một hàm được gọi [ràng buộc thời gian chạy]. Nó không thể được đặt theo phép gán trong khi thực thi và nó có thể khác nhau mỗi khi hàm được gọi. Phương thức const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
3 có thể và các hàm mũi tên không cung cấp ràng buộc const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 của riêng chúng [nó giữ lại giá trị const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 của ngữ cảnh từ vựng kèm theo]this
Ở chế độ không nghiêm ngặt,
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 luôn là một tham chiếu đến một đối tượng. Ở chế độ nghiêm ngặt, nó có thể là bất kỳ giá trị nào. Để biết thêm thông tin về cách xác định giá trị, hãy xem mô tả bên dướiGiá trị của
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 phụ thuộc vào ngữ cảnh mà nó xuất hiện. chức năng, lớp hoặc toàn cầuBên trong một hàm, giá trị của
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 phụ thuộc vào cách hàm được gọi. Hãy nghĩ về const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 như một tham số ẩn của một hàm — giống như các tham số được khai báo trong định nghĩa hàm, const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 là một ràng buộc mà ngôn ngữ tạo ra cho bạn khi phần thân của hàm được đánh giáĐối với một chức năng điển hình, giá trị của
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 là đối tượng mà chức năng được truy cập trên. Nói cách khác, nếu lệnh gọi hàm có dạng function getThisStrict[] {
"use strict"; // Enter strict mode
return this;
}
// Only for demonstration — you should not mutate built-in prototypes
Number.prototype.getThisStrict = getThisStrict;
console.log[typeof [1].getThisStrict[]]; // "number"
3, thì const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 đề cập đến function getThisStrict[] {
"use strict"; // Enter strict mode
return this;
}
// Only for demonstration — you should not mutate built-in prototypes
Number.prototype.getThisStrict = getThisStrict;
console.log[typeof [1].getThisStrict[]]; // "number"
5. Ví dụfunction getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
Lưu ý cách chức năng giống nhau, nhưng dựa trên cách nó được gọi, giá trị của
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 là khác nhau. Điều này tương tự như cách các tham số chức năng hoạt độngGiá trị của
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 không phải là đối tượng có chức năng như một thuộc tính riêng, mà là đối tượng được sử dụng để gọi hàm. Bạn có thể chứng minh điều này bằng cách gọi một phương thức của một đối tượng trong chuỗi nguyên mẫuconst obj3 = {
__proto__: obj1,
name: "obj3",
};
console.log[obj3.getThis[]]; // { name: 'obj3' }
Giá trị của
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 luôn thay đổi dựa trên cách một hàm được gọi, ngay cả khi hàm được xác định trên một đối tượng khi tạoconst obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
Nếu giá trị mà phương thức được truy cập trên đó là giá trị nguyên thủy, thì __________1 cũng sẽ là giá trị nguyên thủy — nhưng chỉ khi hàm ở chế độ nghiêm ngặt
function getThisStrict[] {
"use strict"; // Enter strict mode
return this;
}
// Only for demonstration — you should not mutate built-in prototypes
Number.prototype.getThisStrict = getThisStrict;
console.log[typeof [1].getThisStrict[]]; // "number"
Nếu chức năng được gọi mà không được truy cập trên bất kỳ thứ gì, thì
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 sẽ là console.log[typeof getThisStrict[]]; // "undefined"
1 — nhưng chỉ khi chức năng ở chế độ nghiêm ngặtconsole.log[typeof getThisStrict[]]; // "undefined"
Ở chế độ không nghiêm ngặt, một quy trình đặc biệt được gọi là đảm bảo rằng giá trị của
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 luôn là một đối tượng. Điều này có nghĩa là- Nếu một chức năng được gọi với
1 được đặt thànhconst obj4 = { name: "obj4", getThis[] { return this; }, }; const obj5 = { name: "obj5" }; obj5.getThis = obj4.getThis; console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 hoặcconsole.log[typeof getThisStrict[]]; // "undefined"
6, thìconsole.log[typeof getThisStrict[]]; // "undefined"
1 sẽ được thay thế bằngconst obj4 = { name: "obj4", getThis[] { return this; }, }; const obj5 = { name: "obj5" }; obj5.getThis = obj4.getThis; console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
8console.log[typeof getThisStrict[]]; // "undefined"
- Nếu hàm được gọi với
1 được đặt thành giá trị nguyên thủy, thìconst obj4 = { name: "obj4", getThis[] { return this; }, }; const obj5 = { name: "obj5" }; obj5.getThis = obj4.getThis; console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 sẽ được thay thế bằng đối tượng bao bọc của giá trị nguyên thủyconst obj4 = { name: "obj4", getThis[] { return this; }, }; const obj5 = { name: "obj5" }; obj5.getThis = obj4.getThis; console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
function getThis[] {
return this;
}
// Only for demonstration — you should not mutate built-in prototypes
Number.prototype.getThis = getThis;
console.log[typeof [1].getThis[]]; // "object"
console.log[getThis[] === globalThis]; // true
Trong các lời gọi hàm điển hình, ___________1 được truyền hoàn toàn như một tham số thông qua tiền tố của hàm [phần trước dấu chấm]. Bạn cũng có thể đặt rõ ràng giá trị của
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 bằng cách sử dụng các phương thức function getThis[] {
return this;
}
// Only for demonstration — you should not mutate built-in prototypes
Number.prototype.getThis = getThis;
console.log[typeof [1].getThis[]]; // "object"
console.log[getThis[] === globalThis]; // true
3, function getThis[] {
return this;
}
// Only for demonstration — you should not mutate built-in prototypes
Number.prototype.getThis = getThis;
console.log[typeof [1].getThis[]]; // "object"
console.log[getThis[] === globalThis]; // true
4 hoặc function getThis[] {
return this;
}
// Only for demonstration — you should not mutate built-in prototypes
Number.prototype.getThis = getThis;
console.log[typeof [1].getThis[]]; // "object"
console.log[getThis[] === globalThis]; // true
5. Sử dụng function getThis[] {
return this;
}
// Only for demonstration — you should not mutate built-in prototypes
Number.prototype.getThis = getThis;
console.log[typeof [1].getThis[]]; // "object"
console.log[getThis[] === globalThis]; // true
6, bạn có thể tạo một hàm mới với một giá trị cụ thể là const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 mà không thay đổi bất kể hàm đó được gọi như thế nào. Khi sử dụng các phương thức này, các quy tắc thay thế const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 ở trên vẫn được áp dụng nếu hàm không nghiêm ngặtgọi lại
Khi một chức năng được truyền dưới dạng gọi lại, giá trị của
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 phụ thuộc vào cách gọi lại, được xác định bởi người triển khai API. Các cuộc gọi lại thường được gọi với giá trị const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 là console.log[typeof getThisStrict[]]; // "undefined"
1 [gọi nó trực tiếp mà không gắn nó với bất kỳ đối tượng nào], có nghĩa là nếu chức năng không nghiêm ngặt, giá trị của const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 là đối tượng toàn cục [console.log[typeof getThisStrict[]]; // "undefined"
8]. Đây là trường hợp của , hàm tạo function logThis[] {
"use strict";
console.log[this];
}
[1, 2, 3].forEach[logThis]; // undefined, undefined, undefined
4, v.v.function logThis[] {
"use strict";
console.log[this];
}
[1, 2, 3].forEach[logThis]; // undefined, undefined, undefined
Một số API cho phép bạn đặt giá trị
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 cho các lệnh gọi lại. Ví dụ: tất cả các phương thức mảng lặp và các phương thức liên quan như function logThis[] {
"use strict";
console.log[this];
}
[1, 2, 3].forEach[logThis]; // undefined, undefined, undefined
6 đều chấp nhận tham số tùy chọn function logThis[] {
"use strict";
console.log[this];
}
[1, 2, 3].forEach[logThis]; // undefined, undefined, undefined
7[1, 2, 3].forEach[logThis, { name: "obj" }];
// { name: 'obj' }, { name: 'obj' }, { name: 'obj' }
Đôi khi, một cuộc gọi lại được gọi với giá trị
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 khác với giá trị console.log[typeof getThisStrict[]]; // "undefined"
1. Ví dụ: tham số [1, 2, 3].forEach[logThis, { name: "obj" }];
// { name: 'obj' }, { name: 'obj' }, { name: 'obj' }
0 của [1, 2, 3].forEach[logThis, { name: "obj" }];
// { name: 'obj' }, { name: 'obj' }, { name: 'obj' }
1 và tham số [1, 2, 3].forEach[logThis, { name: "obj" }];
// { name: 'obj' }, { name: 'obj' }, { name: 'obj' }
2 của [1, 2, 3].forEach[logThis, { name: "obj" }];
// { name: 'obj' }, { name: 'obj' }, { name: 'obj' }
3 đều được gọi với ____0_______1 được đặt thành đối tượng mà thuộc tính được phân tích cú pháp/đánh số thứ tự thuộc vềchức năng mũi tên
Trong các hàm mũi tên,
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 giữ lại giá trị của ngữ cảnh từ vựng kèm theo của const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1. Nói cách khác, khi đánh giá phần thân của hàm mũi tên, ngôn ngữ không tạo ràng buộc const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 mớiVí dụ: trong mã toàn cầu,
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 luôn là console.log[typeof getThisStrict[]]; // "undefined"
8 bất kể mức độ nghiêm ngặt, do ràng buộcconst globalObject = this;
const foo = [] => this;
console.log[foo[] === globalObject]; // true
Các hàm mũi tên tạo một bao đóng đối với giá trị
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 của phạm vi xung quanh nó, có nghĩa là các hàm mũi tên hoạt động như thể chúng được "tự động liên kết" — bất kể nó được gọi như thế nào, thì const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 được đặt thành giá trị như khi hàm được tạo [trong . Điều tương tự cũng áp dụng cho các chức năng mũi tên được tạo bên trong các chức năng khác. const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 của họ vẫn là ngữ cảnh từ vựng kèm theo. Ngoài ra, khi gọi các hàm mũi tên bằng cách sử dụng
const globalObject = this;
const foo = [] => this;
console.log[foo[] === globalObject]; // true
3, const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
3 hoặc const globalObject = this;
const foo = [] => this;
console.log[foo[] === globalObject]; // true
5, tham số function logThis[] {
"use strict";
console.log[this];
}
[1, 2, 3].forEach[logThis]; // undefined, undefined, undefined
7 sẽ bị bỏ qua. Tuy nhiên, bạn vẫn có thể truyền các đối số khác bằng các phương thức nàyfunction getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
0nhà xây dựng
Khi một hàm được sử dụng như một hàm tạo [với từ khóa
const globalObject = this;
const foo = [] => this;
console.log[foo[] === globalObject]; // true
7], thì ___________1 của nó được liên kết với đối tượng mới đang được tạo, bất kể hàm tạo được truy cập trên đối tượng nào. Giá trị của const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 trở thành giá trị của biểu thức const globalObject = this;
const foo = [] => this;
console.log[foo[] === globalObject]; // true
7 trừ khi hàm tạo trả về một giá trị không nguyên thủy khácfunction getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
1Trong ví dụ thứ hai [
function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
01], vì một đối tượng đã được trả lại trong quá trình xây dựng nên đối tượng mới mà const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 nhất định sẽ bị loại bỏ. [Điều này về cơ bản làm cho câu lệnh function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
03 chết mã. Nó không thực sự chết vì nó được thực thi, nhưng nó có thể bị loại bỏ mà không có tác động bên ngoài. ]siêu
Khi một hàm được gọi ở dạng
function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
04, thì giá trị _______1 bên trong hàm ___________06 có cùng giá trị với giá trị _______1 xung quanh lệnh gọi _______15_______04 và thường không bằng với đối tượng mà ___________09 đề cập đến. Điều này là do function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
10 không phải là quyền truy cập thành viên đối tượng như những truy cập ở trên — đó là một cú pháp đặc biệt với các quy tắc ràng buộc khác nhau. Ví dụ, xemMột lớp có thể được chia thành hai bối cảnh. tĩnh và ví dụ. Hàm tạo, phương thức và trình khởi tạo trường đối tượng [công khai hoặc riêng tư] thuộc về ngữ cảnh đối tượng. Các phương thức tĩnh, bộ khởi tạo trường tĩnh và khối khởi tạo tĩnh thuộc về ngữ cảnh tĩnh. Giá trị
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 khác nhau trong từng ngữ cảnhCác hàm tạo của lớp luôn được gọi với ____66_______7, vì vậy hành vi của chúng giống như. giá trị
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 là phiên bản mới được tạo. Các phương thức của lớp hoạt động giống như các phương thức trong nghĩa đen của đối tượng - giá trị const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 là đối tượng mà phương thức được truy cập trên đó. Nếu phương thức không được chuyển sang đối tượng khác, thì const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 thường là một thể hiện của lớpCác phương thức tĩnh không phải là thuộc tính của
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1. Chúng là thuộc tính của chính lớp đó. Do đó, chúng thường được truy cập trên lớp và const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 là giá trị của lớp [hoặc một lớp con]. Các khối khởi tạo tĩnh cũng được đánh giá với const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 được đặt thành lớp hiện tạiTrình khởi tạo trường cũng được đánh giá trong ngữ cảnh của lớp. Các trường phiên bản được đánh giá với ____0_______1 được đặt thành phiên bản đang được xây dựng. Các trường tĩnh được đánh giá với
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 được đặt thành lớp hiện tại. Đây là lý do tại sao các hàm mũi tên trong bộ khởi tạo trường làfunction getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
2Các hàm tạo của lớp dẫn xuất
Không giống như các hàm tạo của lớp cơ sở, các hàm tạo dẫn xuất không có ràng buộc
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 ban đầu. Việc gọi function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
23 tạo ra một ràng buộc const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 bên trong hàm tạo và về cơ bản có tác dụng đánh giá dòng mã sau, trong đó function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
25 là lớp cơ sởfunction getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
3Cảnh báo. Đề cập đến
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 trước khi gọi function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
23 sẽ gây ra lỗiCác lớp dẫn xuất không được trả về trước khi gọi
function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
23, trừ khi hàm tạo trả về một đối tượng [vì vậy giá trị const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 bị ghi đè] hoặc lớp không có hàm tạo nào cảfunction getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
4Trong bối cảnh thực thi toàn cầu [bên ngoài bất kỳ chức năng hoặc lớp nào; có thể nằm trong các khối hoặc được xác định trong phạm vi toàn cầu], giá trị
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 phụ thuộc vào bối cảnh thực thi mà tập lệnh chạy trong đó. Giống như , giá trị const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 được xác định bởi môi trường thời gian chạy [người gọi]Ở cấp cao nhất của tập lệnh,
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 đề cập đến console.log[typeof getThisStrict[]]; // "undefined"
8 cho dù ở chế độ nghiêm ngặt hay không. Điều này thường giống với đối tượng toàn cầu — ví dụ: nếu nguồn được đặt bên trong phần tử HTML function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
34 và được thực thi dưới dạng tập lệnh, thì function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
35Ghi chú.
console.log[typeof getThisStrict[]]; // "undefined"
8 nói chung là khái niệm giống như đối tượng toàn cục [i. e. thêm thuộc tính vào console.log[typeof getThisStrict[]]; // "undefined"
8 biến chúng thành biến toàn cục] — đây là trường hợp của trình duyệt và Nút — nhưng máy chủ được phép cung cấp giá trị khác cho console.log[typeof getThisStrict[]]; // "undefined"
8 không liên quan đến đối tượng toàn cụcfunction getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
5Nếu nguồn được tải dưới dạng một mô-đun [đối với HTML, điều này có nghĩa là thêm
function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
39 vào thẻ function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
34], thì const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 luôn là console.log[typeof getThisStrict[]]; // "undefined"
1 ở cấp cao nhấtNếu nguồn được thực thi với
function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
43, thì const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 giống với ngữ cảnh kèm theo cho hoặc console.log[typeof getThisStrict[]]; // "undefined"
8 [như thể nó được chạy trong một tập lệnh toàn cầu riêng biệt] để đánh giá gián tiếpfunction getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
6Lưu ý rằng một số mã nguồn, trong khi trông giống như phạm vi toàn cầu, thực sự được bọc trong một hàm khi thực thi. Ví dụ, nút. js Các mô-đun CommonJS được bao bọc trong một hàm và được thực thi với giá trị
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 được đặt thành function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
47. được thực thi với const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 được đặt thành phần tử mà chúng được gắn vàoCác ký tự đối tượng không tạo phạm vi
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 — chỉ các hàm [phương thức] được xác định trong đối tượng mới làm. Sử dụng const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 trong một đối tượng theo nghĩa đen sẽ kế thừa giá trị từ phạm vi xung quanhfunction getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
7Giá trị của
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 phụ thuộc vào cách hàm được gọi chứ không phải cách nó được định nghĩafunction getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
8Sử dụng
const globalObject = this;
const foo = [] => this;
console.log[foo[] === globalObject]; // true
3 và const globalObject = this;
const foo = [] => this;
console.log[foo[] === globalObject]; // true
5, bạn có thể chuyển giá trị của const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 như thể đó là một tham số thựcfunction getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
9Ở chế độ không nghiêm ngặt, nếu một hàm được gọi với giá trị
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 không phải là đối tượng, thì giá trị const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 được thay thế bằng một đối tượng. console.log[typeof getThisStrict[]]; // "undefined"
6 và console.log[typeof getThisStrict[]]; // "undefined"
1 trở thành console.log[typeof getThisStrict[]]; // "undefined"
8. Các số nguyên thủy như function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
60 hoặc function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
61 được chuyển đổi thành một đối tượng bằng cách sử dụng hàm tạo có liên quan, vì vậy số nguyên thủy function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
60 được chuyển đổi thành lớp bao bọc function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
63 và chuỗi function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
61 thành lớp bao bọc function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
65const obj3 = {
__proto__: obj1,
name: "obj3",
};
console.log[obj3.getThis[]]; // { name: 'obj3' }
0Việc gọi
function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
66 tạo một hàm mới có cùng nội dung và phạm vi như function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
67, nhưng giá trị của const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 bị ràng buộc vĩnh viễn với đối số đầu tiên của function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
69, bất kể hàm đó được gọi như thế nàoconst obj3 = {
__proto__: obj1,
name: "obj3",
};
console.log[obj3.getThis[]]; // { name: 'obj3' }
1Các hàm mũi tên tạo các bao đóng trên giá trị
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 của bối cảnh thực thi kèm theo. Trong ví dụ sau, chúng ta tạo function getThisStrict[] {
"use strict"; // Enter strict mode
return this;
}
// Only for demonstration — you should not mutate built-in prototypes
Number.prototype.getThisStrict = getThisStrict;
console.log[typeof [1].getThisStrict[]]; // "number"
5 bằng phương thức function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
72 trả về một hàm trả về giá trị của const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1. Hàm được trả về được tạo dưới dạng hàm mũi tên, vì vậy ____________1 của nó được liên kết vĩnh viễn với _________1 của hàm kèm theo của nó. Giá trị của const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 bên trong function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
72 có thể được đặt trong lệnh gọi, từ đó đặt giá trị trả về của hàm được trả vềconst obj3 = {
__proto__: obj1,
name: "obj3",
};
console.log[obj3.getThis[]]; // { name: 'obj3' }
2Chúng ta có thể gọi
function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
72 như một phương thức của function getThisStrict[] {
"use strict"; // Enter strict mode
return this;
}
// Only for demonstration — you should not mutate built-in prototypes
Number.prototype.getThisStrict = getThisStrict;
console.log[typeof [1].getThisStrict[]]; // "number"
5, đặt const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 bên trong cơ thể thành function getThisStrict[] {
"use strict"; // Enter strict mode
return this;
}
// Only for demonstration — you should not mutate built-in prototypes
Number.prototype.getThisStrict = getThisStrict;
console.log[typeof [1].getThisStrict[]]; // "number"
5. Hàm trả về được gán cho một biến function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
82. Bây giờ, khi gọi function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
82, giá trị của const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 được trả về vẫn là giá trị được đặt bởi lệnh gọi tới function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
72, là function getThisStrict[] {
"use strict"; // Enter strict mode
return this;
}
// Only for demonstration — you should not mutate built-in prototypes
Number.prototype.getThisStrict = getThisStrict;
console.log[typeof [1].getThisStrict[]]; // "number"
5. Nếu hàm được trả về không phải là hàm mũi tên, các lệnh gọi như vậy sẽ khiến giá trị const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 thành console.log[typeof getThisStrict[]]; // "undefined"
8 hoặc console.log[typeof getThisStrict[]]; // "undefined"
1 ở chế độ nghiêm ngặtconst obj3 = {
__proto__: obj1,
name: "obj3",
};
console.log[obj3.getThis[]]; // { name: 'obj3' }
3Nhưng hãy cẩn thận nếu bạn hủy liên kết phương thức của
function getThisStrict[] {
"use strict"; // Enter strict mode
return this;
}
// Only for demonstration — you should not mutate built-in prototypes
Number.prototype.getThisStrict = getThisStrict;
console.log[typeof [1].getThisStrict[]]; // "number"
5 mà không gọi nó, bởi vì function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
72 vẫn là một phương thức có giá trị const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 thay đổi. Gọi function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
93 trong ví dụ sau trả về console.log[typeof getThisStrict[]]; // "undefined"
8, vì nó theo sau const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 từ function getThis[] {
return this;
}
const obj1 = { name: "obj1" };
const obj2 = { name: "obj2" };
obj1.getThis = getThis;
obj2.getThis = getThis;
console.log[obj1.getThis[]]; // { name: 'obj1', getThis: [Function: getThis] }
console.log[obj2.getThis[]]; // { name: 'obj2', getThis: [Function: getThis] }
96, là console.log[typeof getThisStrict[]]; // "undefined"
8 vì nó được gọi mà không được gắn với bất kỳ đối tượng nàoconst obj3 = {
__proto__: obj1,
name: "obj3",
};
console.log[obj3.getThis[]]; // { name: 'obj3' }
4Hành vi này rất hữu ích khi xác định các cuộc gọi lại. Thông thường, mỗi biểu thức hàm tạo ràng buộc
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 của riêng nó, làm mờ giá trị const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 của phạm vi phía trên. Giờ đây, bạn có thể định nghĩa các hàm dưới dạng các hàm mũi tên nếu bạn không quan tâm đến giá trị const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 và chỉ tạo các ràng buộc const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 khi bạn thực hiện [e. g. trong các phương thức của lớp]. Thấyconst obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 trong getters và setters dựa trên đối tượng mà thuộc tính được truy cập, chứ không phải thuộc tính được xác định trên đối tượng nào. Hàm được sử dụng làm getter hoặc setter có ___________1 ràng buộc với đối tượng mà thuộc tính được đặt hoặc nhậnconst obj3 = {
__proto__: obj1,
name: "obj3",
};
console.log[obj3.getThis[]]; // { name: 'obj3' }
5Khi một chức năng được sử dụng làm trình xử lý sự kiện, thì ____________1 của nó được đặt thành phần tử mà trình nghe được đặt trên đó [một số trình duyệt không tuân theo quy ước này đối với trình nghe được thêm động bằng các phương thức khác với _______18____06]
const obj3 = {
__proto__: obj1,
name: "obj3",
};
console.log[obj3.getThis[]]; // { name: 'obj3' }
6Khi mã được gọi từ một inline , ____________1 của nó được đặt thành phần tử DOM mà người nghe được đặt trên đó
const obj3 = {
__proto__: obj1,
name: "obj3",
};
console.log[obj3.getThis[]]; // { name: 'obj3' }
7Cảnh báo trên cho thấy
const obj3 = {
__proto__: obj1,
name: "obj3",
};
console.log[obj3.getThis[]]; // { name: 'obj3' }
08. Tuy nhiên, lưu ý rằng chỉ có mã bên ngoài được thiết lập theo cách nàyconst obj3 = {
__proto__: obj1,
name: "obj3",
};
console.log[obj3.getThis[]]; // { name: 'obj3' }
8Trong trường hợp này, hàm bên trong
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 không được đặt, vì vậy nó trả về đối tượng toàn cục/cửa sổ [i. e. đối tượng mặc định ở chế độ không nghiêm ngặt khi mà lệnh gọi không đặt ___0_______1]Cũng giống như với các hàm thông thường, giá trị của
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 trong các phương thức phụ thuộc vào cách chúng được gọi. Đôi khi sẽ hữu ích khi ghi đè hành vi này để const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 trong các lớp luôn đề cập đến thể hiện của lớp. Để đạt được điều này, hãy liên kết các phương thức lớp trong hàm tạoconst obj3 = {
__proto__: obj1,
name: "obj3",
};
console.log[obj3.getThis[]]; // { name: 'obj3' }
9Ghi chú. Các lớp học luôn ở chế độ nghiêm ngặt. Gọi các phương thức với một ____________1 không xác định sẽ gây ra lỗi nếu phương thức đó cố truy cập các thuộc tính trên
const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1Tuy nhiên, lưu ý rằng các phương thức liên kết tự động gặp phải vấn đề tương tự như. mỗi thể hiện của lớp sẽ có bản sao phương thức riêng, giúp tăng mức sử dụng bộ nhớ. Chỉ sử dụng nó khi thực sự cần thiết. Bạn cũng có thể bắt chước việc thực hiện. xác định thuộc tính là một getter trả về một hàm bị ràng buộc khi được truy cập và lưu nó, để hàm chỉ được tạo một lần và chỉ được tạo khi cần thiết
Mặc dù các câu lệnh
const obj3 = {
__proto__: obj1,
name: "obj3",
};
console.log[obj3.getThis[]]; // { name: 'obj3' }
17 không được dùng nữa và không có sẵn ở chế độ nghiêm ngặt, nhưng chúng vẫn đóng vai trò là ngoại lệ đối với các quy tắc ràng buộc const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 bình thường. Nếu một hàm được gọi trong câu lệnh const obj3 = {
__proto__: obj1,
name: "obj3",
};
console.log[obj3.getThis[]]; // { name: 'obj3' }
17 và hàm đó là thuộc tính của đối tượng phạm vi, thì giá trị const obj4 = {
name: "obj4",
getThis[] {
return this;
},
};
const obj5 = { name: "obj5" };
obj5.getThis = obj4.getThis;
console.log[obj5.getThis[]]; // { name: 'obj5', getThis: [Function: getThis] }
1 được đặt cho đối tượng phạm vi, như thể tiền tố const obj3 = {
__proto__: obj1,
name: "obj3",
};
console.log[obj3.getThis[]]; // { name: 'obj3' }
21 tồn tại