google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Gán chồng đối tượng this trong JavaScript Bài viết này sẽ hướng dẫn bạn một vài cách thức để gán chồng đối tượng đặc biệt this trong ngôn ngữ lập trình JavaScript. Bài viết này sẽ chỉ ra nguyên nhân bạn không thể dùng toán tử gán bình thường (dấu =) cho đối tượng this cùng với biện pháp khắc phục. Nói chung, đây là một bài viết đáng đọc để bạn hiểu hơn về lập trình hướng đối tượng trong JavaScript.


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é!
Thử iPage miễn phí cho năm đầu tiên NGAY

Have you ever experience a time when the calling object ‘this’ needed to be override and somehow you just couldn’t do that? You tried left hand assignment but it just won’t work? wondering how can ‘this’ object be overridden in JavaScript but can’t find the correct answer on Google? This article will explain how this can be done.

Left hand assignment

Ever tried left hand assignment to override ‘this’ object?

var obj = document.getElementById("button");
this = obj

It will fail and gives you ‘invalid assignment left-hand side‘.

Using Call() to override ‘this’ object

There is a function in JavaScript, Call() which can help you override ‘this’ object or specify the ‘this’ object. Let’s consider the following example,

function example(a,b)
{
	this.value == 'button'?alert(a):alert(b);
}
var textobj = document.getElementById('input_relink');
example.call(textobj, 'It is a button!', 'It is not a button');

We look at a text box and check whether the user key in ‘button’. But we uses ‘this’ object to verify the input text in the function example which takes in two parameter as shown above. The Call() method is used whenever you wish to override ‘this’ object which is placed at the first parameter and the rest of the parameter will be exactly the same as the method parameter. Hence, we used the function in this way for the above example because it has two parameter.

//override 'this' with object 'textobj'
example.call(textobj, 'It is a button!', 'It is not a button');

If we have another function which takes in 3 parameter, we will do this.

function example(a,b, c)
{
	this.value == 'button'?alert(a):alert(b);
	alert(c);
}
var textobj = document.getElementById('input_relink');
example.call(textobj, 'It is a button!', 'It is not a button', 'Completed');

You can also used it without parameter like the one illustrated below,

var o = { x: 15 };
function f()
{
    alert(this.x);
}
f.call(o);

Using Call() method restrict your variable given on the method. On the other hand, we can use another method Apply() which override the ‘this’ object giving the second parameter as an array.

Using Apply() method to override ‘this’ object

The difference between call and apply method is the parameter needed to use them. For Call() method it depends on the parameter of the function as shown above. But for Apply() method it can have more than the amount of parameter in the function (since it takes in an array). Similarly, the first parameter of both methods are used to override ‘this’ object. Let’s consider the following example,

var o = { x: 8 };
function example()
{
    // arguments[0] == object
	var sum = 0;
    for(var i = 1; i < arguments.length; i++)
    {
       sum+=arguments[i]
    }
	alert(sum+ this.x);
}
example.apply(o, 1, 2, 3, 4)

I am using the depreciated argument array to look through the argument being passed into the function, sum them up, alert to the user. Similarly, i can do this using an array.

var o = { x: 8 };
function example(a, b, c)
{
	alert(this.x+a+b+c);
}
example.apply(o, [1,2,3])

Pretty simple isn’t it?

iPhoneKer.com
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

JavaScript theo ngày


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web