google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Các khái niệm JavaScript: Đơn giản mà tốt nhất Nếu thường xem phim truyện trên kênh truyền hình HBO hẳn bạn sẽ quen với câu khẩu hiệu "Simply the Best" mà jsB@nk đang sử dụng để đặt cho tiều đề bài viết này. jsB@nk muốn mượn khẩu hiệu này để nói lên tính chất của bài viết: nội dung này cung cấp cho bạn các khái niệm rất cơ bản về JavaScript, có thể nhanh chóng và dễ dàng tiếp cận, nắm vững ngôn ngữ lập trình web JavaScript.

Bài viết cung cấp các hướng dẫn chi tiết cùng với mã nguồn ví dụ JavaScript mẫu kèm theo. Hiện tại bài viết này có 5 danh mục và sẽ được cập nhật liên tục, trong khi chờ đợi, bạn có thể xem qua:
- Phần 1: Lớp trong JavaScript
- Phần 2: Kế thừa trong JavaScript
- Phần 3: JavaScript và JSON
- Phần 4: Thuộc tính Prototype
- Phần 5: Tầm vực trong JavaScript

Các bài viết hướng dẫn làm quen với JavaScript khác có trên jsB@nk:
- Hàm JavaScript & Biểu thức so trùng: Vài ví dụ cơ bản
- Tổng quan về Prototype của JavaScript
- 5 kĩ thuật kế thừa trong JavaScript nên nắm vững
- Kiểu và Đối tượng đơn giản trong LTHĐT JavaScript
- 10 eBook tốt nhất người mới học JavaScript nên đọ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

While the Javascript language offers many of the constructs required for object-oriented programming, they remain largely unused. Today well take a look at how to start with object-oriented programing in Javascript. by defining a class in Javascript. Well use the simple HTML file to call our script file,

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title>Hello Javascript!</title>

        <script type="text/javascript" src="MyScript.js"></script>
    </head>
    <body>
        Hello Javascript!
    </body>

</html>

Now lets create a class definition (in MyScript.js). Note that there is no class keyword in Javascript, the class definition is just a function definition,

function MyClass() {

    // Public field
    this.aPublicField = "This is a public field of the type MyClass";

    // Private field
    var aPrivateField = "This is a private field of the type MyClass";

    // Public method

    this.aPublicMethod = function() {
        // Use the private method
        if (aPrivateMethod()) { return this.aPublicField; }
        else { return aPrivateField; }
    }

    // Private method
    function aPrivateMethod() {
        return true;
    }

    // Oops! We can expose the private field

    this.exposePrivateField = aPrivateField;
    // and the private method
    this.exposePrivateMethod = aPrivateMethod;
}

// Create an instance of MyClass using the new keyword
var myclass = new MyClass();

// Get the public field
alert(myclass.aPublicField);

// Call the public method
alert(myclass.aPublicMethod());

// Call the private field  cant get to them directly

alert(myclass.exposePrivateField);
alert(myclass.exposePrivateMethod());

So its pretty simple to create a class in Javascript. We can also create a runtime field for the class,

// Create an instance of MyClass
var myclass = new MyClass();

// Create a field at runtime
myclass.aRuntimeField = "This is a runtime field of the type MyClass";

// View the field value
alert(myclass.aRuntimeField);

Part 2: Javascript Inheritance

Now that we created a base Javascript class in the first post, lets inherit from it to get a derived class.

// Define the derived function (remember there is no 'class' keyword in Javascript)
function MyDerivedClass() { }

// Derive from MyClass, equivalent to > public class MyDerivedClass : MyClass
MyDerivedClass.prototype = new MyClass();


// Create an instance of the derived class
var myderivedClass = new MyDerivedClass();

// Get the public field
alert(myderivedClass.aPublicField);

// Call the public method
alert(myderivedClass.aPublicMethod());

Woah! That was simple! Notice that MyClasss private methods and the runtime field (aRuntimeField) we gave to MyClass in the last post is not available to MyDerivedClass.

Lets override the base classes public fields and methods and see what happens,

// Define the derived function (remember there is no 'class' keyword in Javascript
function MyDerivedClass() {

    // Override the base's public field
    this.aPublicField = "This is a public field of the type MyDerivedClass";

    // Override the base's public method
    this.aPublicMethod = function() { return this.aPublicField; }
}


// Derive from MyClass, equivalent to > public class MyDerivedClass : MyClass
MyDerivedClass.prototype = new MyClass();

// Create an instance of the derived class
var myderivedClass = new MyDerivedClass();

// Get the public field
alert(myderivedClass.aPublicField);

// Call the public method
alert(myderivedClass.aPublicMethod());

Youll see that the base classes public fields and methods have been overridden in the derived class.

We can obviously derive again,

// Derive from MyDerivedClass
function MyDerivedDerivedClass() {

    // Override the base's public field
    this.aPublicField = "This is a public field of the type MyDerivedDerivedClass";

    // Override the base's public method
    this.aPublicMethod = function() { return this.aPublicField; }
}


// Derive from MyDerivedClass, equivalent to > public class MyDerivedDerivedClass : MyDerivedClass
MyDerivedDerivedClass.prototype = new MyDerivedClass();

// Create an instance of the derived class
var myderivedderivedClass = new MyDerivedDerivedClass();

// Call the public method
alert(myderivedderivedClass.aPublicField);
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