twitter@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.


Tạo video bằng AI chỉ với giọng nói hoặc văn bản



Ứng dụng video AI MIỄN PHÍ hàng đầu của bạn! Tự động hóa video AI đầu tiên của bạn. Tạo Video Chuyên Nghiệp Của Bạn Trong 5 Phút Bằng AI Không Cần Thiết Bị Hoặc Kỹ Năng Chỉnh Sửa Video. Sản xuất video dễ dàng dành cho nhà tiếp thị nội dung.
Ứng dụng video AI KHÔNG GIỚI HẠN miễn phí trong tay bạn

Using a Constructor Function.

Let's use our previous constructor function and build upon it so it performs some basic (but dynamic) operations when we instantiate it.

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

Just like any JavaScript function, we can use arguments with our constructor function;

function myObject(what){
	this.iAm = what;
	this.whatAmI = function(language){
		alert('I am ' + this.iAm + ' of the ' + language + ' language');
	};
};

Now let's instantiate our object and call its whatAmI method, filling in the required fields as we do so.

    var myNewObject = new myObject('an object');
    myNewObject.whatAmI('JavaScript');

This will alert 'I am an object of the JavaScript language.'

To Instantiate or not to Instantiate

I mentioned earlier about the differences between Object Constructors and Object Literals and that when a change is made to an Object Literal it affects that object across the entire script, whereas when a Constructor function is instantiated and then a change is made to that instance, it won't affect any other instances of that object. Let's try an example;

First we will create an Object literal;

	var myObjectLiteral = {
    	myProperty : 'this is a property'
    }
    
    //alert current myProperty
    alert(myObjectLiteral.myProperty); //this will alert 'this is a property'
    
    //change myProperty
    myObjectLiteral.myProperty = 'this is a new property';
    
    //alert current myProperty
    alert(myObjectLiteral.myProperty); //this will alert 'this is a new property', as expected

Even if you create a new variable and point it towards the object, it will have the same effect.

	var myObjectLiteral = {
    	myProperty : 'this is a property'
    }
    
    //alert current myProperty
    alert(myObjectLiteral.myProperty); //this will alert 'this is a property'
    
    //define new variable with object as value
    var sameObject = myObjectLiteral;
    
    //change myProperty
    myObjectLiteral.myProperty = 'this is a new property';
    
    //alert current myProperty
    alert(sameObject.myProperty); //this will still alert 'this is a new property'

Now let's try a similar exercise with a Constructor function.

	//this is one other way of creating a Constructor function
	var myObjectConstructor = function(){
    	this.myProperty = 'this is a property'
    }
    
    //instantiate our Constructor
    var constructorOne = new myObjectConstructor();
    
    //instantiate a second instance of our Constructor
    var constructorTwo = new myObjectConstructor();
    
    //alert current myProperty of constructorOne instance
    alert(constructorOne.myProperty); //this will alert 'this is a property'
     
     //alert current myProperty of constructorTwo instance
    alert(constructorTwo.myProperty); //this will alert 'this is a property'    

So as expected, both return the correct value, but let's change the myProperty for one of the instances.

	//this is one other way of creating a Constructor function
	var myObjectConstructor = function(){
    	this.myProperty = 'this is a property'
    }
    
    //instantiate our Constructor
    var constructorOne = new myObjectConstructor();
    
    //change myProperty of the first instance
    constructorOne.myProperty = 'this is a new property';
    
    //instantiate a second instance of our Constructor
    var constructorTwo = new myObjectConstructor();
    
    //alert current myProperty of constructorOne instance
    alert(constructorOne.myProperty); //this will alert 'this is a new property'
     
     //alert current myProperty of constructorTwo instance
    alert(constructorTwo.myProperty); //this will still alert 'this is a property'    

As you can see from this example, even though we changed the property of constructorOne it didn't affect myObjectConstructor and therefore didn't affect constructorTwo. Even if constructorTwo was instantiated before we changed the myProperty property of constructorOne, it would still not affect the myProperty property of constructorTwo as it is a completely different instance of the object within JavaScript's memory.

So which one should you use? Well it depends on the situation, if you only need one object of its kind for your script (as you will see in our example at the end of this article), then use an object literal, but if you need several instances of an object, where each instance is independent of the other and can have different properties or methods depending on the way it's constructed, then use a constructor function.


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web