google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Khái niệm cơ bản về lập trình hướng đối tượng trong JavaScript Lập trình hướng đối tượng (OOP - Object Oriented Programming) đang là xu hướng lập trình phổ biến và được hỗ trợ bởi hầu như tất cả các ngôn ngữ lập trình hiện nay, và JavaScript cũng không phải là một ngoại lệ. Bài viết này nhằm cung cấp một vài khái niệm cơ bản về vấn đề lập trình hướng đối tượng trong ngôn ngữ 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

Object-Oriented Programming is currently one of the most prominent aspects of programming. Many programming courses teach OOP, and PHP5 supports proper OOP. While Javascript as a language is not inherently well-suited for full fledged OOP, it is possible to make it work.

Here is a basic primer in using OOP in Javascript.

Methods vs Objects

In Javascript, both general methods and objects are defined using the function keyword. For example, the method and the object definitions below are both denoted by function.

function add( a, b ) {
	return a+b;
}

function sum( a, b ) {
	this.total = a+b;
}

The difference between a method call and an object instantiation call is the keyword new. The difference is illustrated in the example below:

var addition = add( 1, 2 );
var summation = new sum( 1, 2 );

Fields

In OOP in Javascript, objects have fields that store data. You have already seen an example of this earlier in the sum object, which stores the sum of a+b as total. All of these fields are public - that is to say, modifiable by elements other than the object itself. For example:

var summation = new sum( 1, 2 );
alert(summation.total); // shows 3
summation.total = 5;
alert(summation.total); // shows 5

Methods in Objects

Next, objects can declare their own methods. These methods can be defined within the object definition, or outside of the definition and assigned to a variable within the object definition.

This provides an example of the former:

function sum( a, b ) {
	this.total = add( a, b );
	this.print = function() {
		print(this.total);
	}
}

and this provides an example of the latter:

function sum( a, b ) {
	this.total = add( a, b );
	this.print = print;
}

function print( total ) {
	print(total);
}

Inheritance

Finally, a major part of OOP is inheritance. Inheritance is when a "subclass" takes on all the methods of a "superclass". Inheritance in Javascript is done using two lines:

this.inheritFrom = ;
this.inheritFrom();

Here is an example of the subclass retaining the methods of the superclass while adding methods of its own.

function super() {
	this.alert = function() {
		alert("Hello!");
	}
	this.alert2 = function() {
		alert("Hello2!");
	}
}
function sub() {
	this.inheritFrom = super;
	this.inheritFrom();

	this.alert3 = function() {
		alert("Hello3!");
	}
}
var c = new sub();
c.alert(); // alerts "Hello!"
c2.alert2(); // alerts "Hello2!"
c2.alert3(); // alerts "Hello3!"

While this primer is not necessarily in-depth, it has hopefully given you a basic understanding how OOP works in Javascript.

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