google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Con trỏ trong lập trình hướng đối tượng JavaScript Nếu đã từng làm quen với lập trình hướng đối tượng, hẳn bạn cũng đã biết đến khái niệm con trỏ - dùng để tham chiếu đến một đối tượng có sẵn. Và ngôn ngữ lập trình JavaScript cũng hỗ trợ lập trình hướng đối tượng, hiển nhiên là nó cũng sẽ hỗ trợ loại dữ liệu con trỏ này; nhưng sẽ có một chút rắc rối, nhầm lẫn của loại dữ liệu này trong JavaScript, bởi sự khác nhau về bản chất của JavaScript với các ngôn ngữ lập trình khác. Bạn hãy đọc bài viết hướng dẫn sử dụng JavaScript này nếu muốn quan tâm kĩ hơn về con trỏ 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

Pointers can sometimes be a confusing topic. In JavaScript, it is perhaps even moreso than in other languages. The confusion arises because the var keyword is serving double duty. It is used to define variables, as is commonly realized. But it is also used to make references- or point to- other variables, functions and properties.

In most programming languages, for example C, pointers are symbolized with an asterisk. JavaScript relies on the developer to know which vars are references and which aren't. Why is this important? To put it succinctly, it is the difference between a variable being immutable or dynamic.

An example may be in order. Here's a simple script you can paste into Firebug to see how pointers work.

var marxBros = ['Chico', 'Harpo', 'Groucho']
var marxBrosPointer = marxBros;
marxBrosPointer.push('Zeppo');
console.log(marxBros); // ['Chico', 'Harpo', 'Groucho', 'Zeppo']
console.log(marxBrosPointer); // ['Chico', 'Harpo', 'Groucho', 'Zeppo']

As you can see, marxBros and marxBrosPointer are identical. Anything you do to marxBrosPointer will change marxBros and vice versa. You can think of them like aliases which both reference the same thing.

There is actually no difference now. marxBros is the original and marxBrosPointer is the pointer. But, for all intents and purposes, they are identical. One thing to note is that if you reassign one of them to something else, it does not affect the other.

var marxBros = ['Chico', 'Harpo', 'Groucho']
var marxBrosPointer = marxBros;
marxBros = null;
console.log(marxBrosPointer); //  ["Chico", "Harpo", "Groucho"]

Now let's look at a case where we are not dealing with pointers. Let's do the same thing as our first example, but instead of using an array, we'll use strings. Yes, this makes a big difference.

var marxBros = 'Chico, Harpo, Groucho';
var marxBrosCopy = marxBros;
marxBrosCopy += ', Zeppo';
console.log(marxBros); // 'Chico, Harpo, Groucho'
console.log(marxBrosCopy); // 'Chico, Harpo, Groucho, Zeppo'

When using strings instead of arrays, we find that changes to one do not change the other. Whether you change the original or the copy, they are not linked in the same way arrays and objects are. In the first example, changing either the original or the pointer resulted in changes to both variables. In this case, because strings are immutable in JavaScript, the var keyword copies a string instead of referencing it.

The same is true of numbers:

var numberOfMarxBros = 3;
var numberOfMarxBrosCopy = numberOfMarxBros;
++numberOfMarxBrosCopy;
console.log(numberOfMarxBros); // 3
console.log(numberOfMarxBrosCopy); // 4

Compare this to properties and functions, which use pointers:

// First let's make a cleanHouse function which has an action property
var cleanHouse = function() {
 console.log(arguments.callee.action);
}
cleanHouse.action = 'Doing your dishes...';

// Let's test and make sure it works as expected.
cleanHouse(); // 'Doing your dishes'

// So far so good. Now let's make a pointer to this function.
var actionToTake = cleanHouse;
actionToTake(); // 'Doing your dishes...'

// Now actionToTake is identical to cleanHouse. Any changes to one affect the other.
cleanHouse.action = 'Vacauuming the rug...';
actionToTake(); // 'Vacauuming the rug...'

actionToTake.action = 'Watering the plants...';
cleanHouse(); // 'Watering the plants...';

Here's an example where the reference is lost and it does not achieve the goal:

var cleanHouse = function() {
    console.log('Doing your dishes...');
}

var actionToTake = cleanHouse;

actionToTake(); // 'Doing your dishes...'

// Off to a good start, but here's where it goes wrong:

cleanHouse = function() {
    console.log('Vacauuming the rug...');
};

// Now the reference has been broken and actionToTake no longer refers to cleanHouse.
// Instead, actionToTake refers to, literally, the anonymous function that cleanHouse previously referred to.

actionToTake(); // 'Doing your dishes...'

// cleanHouse now refers to a new separate function.
cleanHouse(); // 'Vacuuming the rug...'

As you can see, it's important to know how to use pointers. It will help you decide when you need a copy or something, or when you need a reference to it.

Feel free to post clarifications or other explanations. This topic is certainly important to becoming an advanced JavaScript developer, yet it is not addressed too often. I think we could all benefit from more clarity on the subject, myself included.

I hope this article has helped shine some light on this issue. Happy coding!

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