google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Bước đầu làm quen với JavaScript hướng đối tượng Trong một vài năm gần đây, ngôn ngữ lập trình web JavaScript phát triển khá mạnh, với sự hỗ trợ của rất nhiều nền tảng như các hệ điều hành di động, các máy tính bảng, các điện thoại thông minh. Từ sự hỗ trợ mạnh mẽ này mà bản thân ngôn ngữ JavaScript cũng có những cải tiến vượt bậc. Và hiện tại ngôn ngữ này đã hỗ trợ lập trình hướng đối tượng khá tốt. Hãy vào trang chi tiết để xem cách tiếp cận cách lập trình hướng đối tượng trong JavaScript nếu bạn chưa quen.


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

Over recent years, JavaScript has increasingly gained popularity, partly due to libraries that are developed to make JavaScript apps/effects easier to create for those who may not have fully grasped the core language yet.

While in the past it was a common argument that JavaScript was a basic language and was very 'slap dash' with no real foundation; this is no longer the case, especially with the introduction of high scale web applications and 'adaptations' such as JSON (JavaScript Object Notation).

JavaScript can have all that an Object-Orientated language has to offer, albeit with some extra effort outside of the scope of this article.

Let's Create an Object

    function myObject(){
    
    };

Congratulations, you just created an object. There are two ways to create a JavaScript object: they are 'Constructor functions' and 'Literal notation'. The one above is a Constructor function, I'll explain what the difference is shortly, but before I do, here is what an Object definition looks like using literal notation.

    var myObject = {
    
    };

Literal is a preferred option for name spacing so that your JavaScript code doesn't interfere (or vice versa) with other scripts running on the page and also if you are using this object as a single object and not requiring more than one instance of the object, whereas Constructor function type notation is preferred if you need to do some initial work before the object is created or require multiple instances of the object where each instance can be changed during the lifetime of the script. Let's continue to build on both our objects simultaneously so we can observe what the differences are.

Defining Methods and Properties

Constructor version:

    function myObject(){
        this.iAm = 'an object';
        this.whatAmI = function(){
            alert('I am ' + this.iAm);
        };
    };

Literal version:

    var myObject = {
        iAm : 'an object',
        whatAmI : function(){
            alert('I am ' + this.iAm);
        }
    }

For each of the objects we have created a property 'iAm' which contains a string value that is used in our objects method 'whatAmI' which alerts a message.

Properties are variables created inside an object and methods are functions created inside an object.

Now is probably as good a time as any to explain how to use properties and methods (although you would already have done so if you are familiar with a library).

To use a property first you type what object it belongs to - so in this case it's myObject - and then to reference its internal properties, you put a full stop and then the name of the property so it will eventually look like myObject.iAm (this will return 'an object').

For methods, it is the same except to execute the method, as with any function, you must put parenthesis after it; otherwise you will just be returning a reference to the function and not what the function actually returns. So it will look like myObject.whatAmI() (this will alert 'I am an object').

Now for the differences:

  • The constructor object has its properties and methods defined with the keyword 'this' in front of it, whereas the literal version does not.
  • In the constructor object the properties/methods have their 'values' defined after an equal sign '=' whereas in the literal version, they are defined after a colon ':'.
  • The constructor function can have (optional) semi-colons ';' at the end of each property/method declaration whereas in the literal version if you have more than one property or method, they MUST be separated with a comma ',', and they CANNOT have semi-colons after them, otherwise JavaScript will return an error.

There is also a difference between the way these two types of object declarations are used.

To use a literally notated object, you simply use it by referencing its variable name, so wherever it is required you call it by typing;

    myObject.whatAmI();

With constructor functions you need to instantiate (create a new instance of) the object first; you do this by typing;

    var myNewObject = new myObject();
    myNewObject.whatAmI();

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