Bài viết này sẽ hướng dẫn bạn chi tiết cách thức và ví dụ mẫu để thực hành kèm theo trong việc sử dụng đối tượng this của ngôn ngữ lập trình JavaScript hướng đối tượng: từ các khái niệm cơ bản như tầm vực, cách khai báo và sử dụng cho đến cách dùng như một tham chiếu.
Vui lòng vào trang chi tiết để xem thêm hoặc đọc các bài viết liên quan khác:
- Đối tượng JavaScript: So sánh và Sao chép
- Bước đầu làm quen với JavaScript hướng đối tượng
- Trắc nghiệm về các đối tượng trong JavaScript
- Kiểu và Đối tượng đơn giản trong LTHĐT JavaScript
- Kĩ thuật lập trình hướng đối tượng trong JavaScript
- Demo
- Phóng to
- Tải lại
- Cửa sổ mới
Miễn phí web hosting 1 năm đầu tại iPage
Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?
Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.
Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
2. Module madness
This one will give you a headache. Examine the following code which uses a module pattern:
window.WhoAmI = "I'm the window object";
var Module = function() {
this.WhoAmI = "I'm the Module object";
function Test() {
this.WhoAmI = "I'm still the Module object";
}
return {
WhoAmI: WhoAmI,
Test: Test
};
}();
alert(Module.WhoAmI); // I'm the Module object
alert(window.WhoAmI); // I'm the Module object
Module.Test();
alert(Module.WhoAmI); // I'm still the Module object
The code looks logical so why is window.WhoAmI
saying its the module object?
We need to remember that we have a self-executing function. Its results are returned to the Module
variable but, when its first run, Module doesnt exist. this
is therefore the global window object. In other words, this.WhoAmI
= window.WhoAmI
= "I'm the Module object"
.
The function returns a JavaScript object with a WhoAmI
property with a value of 'WhoAmI'
. But what does that refer to? In this case, the JavaScript interpreter works up its prototype chain until it magically finds window.WhoAmI
("I'm the Module object"
).
Finally, we run the Test()
method. However, Module
has now been created so, within the Test
function, this
refers to the Module
object so it can correctly set the WhoAmI
property.
In summary, avoid using this
within a module to refer to the module itself! You should never need it.
3. Method misconceptions
Heres another JavaScript pattern which will screw with your synapses:
var myObject = {};
myObject.method = function() {
this.WhoAmI = "I'm myObject.method";
function Test() {
this.WhoAmI = "I'm myObject.method.Test()";
}
Test();
return this.WhoAmI;
};
alert(myObject.method()); // I'm myObject.method
In this example, Test()
is a private function executed within myObject.method()
. At first glance, you would expect this
within Test()
to reference myObject
. It doesnt: it refers to the global window object since its just another function.
If you want to reference myObject
within the private function, youll require a closure, for example:
var myObject = {};
myObject.method = function() {
this.WhoAmI = "I'm myObject.method";
var T = this;
function Test() {
T.WhoAmI = "I'm myObject.method.Test()";
}
Test();
return this.WhoAmI;
};
alert(myObject.method()); // I'm myObject.method.Test()
4. Referencing methods
Heres a little code which, fortunately, will work exactly as you expect:
var myObject = {};
myObject.WhoAmI = "I'm myObject";
myObject.method = function() {
this.WhoAmI = "I'm myObject.method";
};
// examine properties
alert(myObject.WhoAmI); // I'm myObject
myObject.method();
alert(myObject.WhoAmI); // I'm myObject.method
Lets make a minor change and assign myObject.method
to another variable:
// examine properties
alert(myObject.WhoAmI); // I'm myObject
var test = myObject.method;
test();
alert(myObject.WhoAmI); // I'm myObject
Why hasnt myObject.WhoAmI
changed? In this case, the call to test()
acts like a regular function call so this
refers to the window object rather than myObject
.
If you think thats nasty, wait until we take a look at JavaScript event handlers in my next post!
- Lượt gửi (0)
- Mới
Save up to 630$ when buy new iPhone 15
GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project
https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools
chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language
Dall-E-OpenAI.com
Generate creative images automatically with AI
AIVideo-App.com
Render creative video automatically with AI