google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Lập trình hướng đối tượng trong JavaScript: Vài điều cơ bản Lập trình hướng đối tượng - một xu hướng lập trình đang thịnh hành và ngày càng phổ biến, bởi rất nhiều thế mạnh của nó. Và ngôn ngữ lập trình JavaScript cũng đã hỗ trợ xu hướng lập trình này từ rất lâu; nhưng bài viết này chỉ trình bày sơ lược về những khái niệm cơ bản của nó, vui lòng xem bài viết chi tiết để biết thêm.


Nhãn: LTHĐT, cơ bản, xu hướng, thịnh hành, phổ biến, thế mạnh, sơ lược

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

For all those that use Object Oriented Programming know the benefits.  The ability to write reusable code is a real time saver in the long run.  This post will cover how to write the basic structure of Object Oriented Programming in JavaScript.

 

The Basics

For those of you that don't know what Object Oriented Programming (oop) is, it's simply a way of writing code that allows you to reuse the same code in several other projects.  Everything is encapsulated into nice little packages.  This post won't go over the actual theory of OOP, but I would definitely recommend looking into it.  I'm sure if you Googled it, thousands of entries would come up.

 

The Ways

There are a couple of ways to handle creating a class in JavaScript.

  • Placing everything inside a function
  • Placing everything inside an object
  • Using prototype to build a class

Placing Everything Inside a Function

In my opinion this is probably the easiest to read out of all of them.  However it isn't the most efficient from a processor point of view. 

Say were going to build a Dog class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function Dog () {
    this.name = "";
    this.position = 0;
    this.bark = function (){
        alert("wuff");
    };
    this.walk = function(){
        this.position += 1;
        alert("position = "+this.position);
    }
    this.getName = function (){
        return this.name;
    }
}

var dog = new Dog();
dog.name = "Ralph";
dog.bark(); //Popup box with "wuff"
dog.walk(); //Position increases by 1;
alert(dog.getName()); //Outputs dog's name*/

Class methods are simply variables assigned to functions, and public variables simply use "this" infront of their names. If you wish to make a private variable, you would just create a regular variable
using "var" (e.g. var test = 1;)

 

Placing Everything Inside an Object

This is handy if you don't want to instantiate the class every time you want to use it.  The downside is that it makes inheritance a little more difficult.

Here is the same Dog class but using an Object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var Dog = {
    name    :   "",
    position:   0,
    bark    :   function(){ alert("wuff") },
    walk    :   function(){
        this.position += 1;
        alert("position = "+this.position);
    },
    getName :   function(){
        return this.name
    }
}


Dog.name = "Ralph";
Dog.bark(); //Popup box with "wuff"
Dog.walk(); //Position increases by 1;
alert(Dog.getName()); //Outputs dog's name

This method uses JavaScript Object Notation (JSON) to layout the class. Keys go on the left, and values go on the right. Once again methods are simply functions assigned to variables.

 

Using prototype to Build a Class

This is probably the preferred method of OOP in JavaScript.  It's a little harder to read, but say if we were to create thousands of dog objects, this would out perform the other methods.  Using prototypes it doesn't store the functions within the class. This means you aren't creating thousands of functions, you're only create one function that is pointed to a thousand times.  Plus it's easier to deal with inheritance.

Here is the Dog class yet again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function Dog (){}
Dog.prototype.name = "";
Dog.prototype.position = 0;

Dog.prototype.bark = function (){
    alert("wuff");
}
Dog.prototype.walk = function (){
    this.position += 1;
    alert("position = "+this.position);
}
Dog.prototype.getName = function(){
    return this.name;
}

var dog = new Dog();
dog.name = "Ralph";
dog.bark(); //Popup box with "wuff"
dog.walk(); //Position increases by 1;
alert(dog.getName()); //Outputs dog's name

What the prototype keyword is doing here is creating a framework. So when the class is instanciated it will have all the methods and properties that it needs.

 

Here is a Puppy class to show how inheritance is done.  It inherits all the methods of the Dog class, but overwrites the bark method for a more puppy like bark.

1
2
3
4
5
6
7
8
9
10
11
12
13
function Puppy (){}

Puppy.prototype = new Dog();

Puppy.prototype.bark = function (){
    alert("Yelp");
}

var puppy = new Puppy();
puppy.name = "Ralph";
puppy.bark(); //Popup box with "Yelp"
puppy.walk(); //Position increases by 1;
alert(puppy.getName()); //Outputs puppy's name

 

Conclusion

This is by no means a complete tutorial of everything OOP.  This shows the basic structures of how JavaScript can handle OOP.  Out of the three that were outlined, it is probably best to stick with the prototype method. If there are any Actionscript 2 people out there, you'll feel right at home with this method.

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