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

Part 5: Javascript Closures

Closures are a tricky concept in Javascript. They seem deceptively simple but there are a lot of intricacies that can trip up developers. To start understanding closures, we need to start with scope in Javascript, a function has access to its parents context, grandparents  context, etc. up the scope chain. To see this try the code,

var X = "window";

function outermost() {
    var Y = "outermost";

    function inner() {
        // Knows about the parent function's Y
        alert(Y);

        function innermost() {
            // Knows about the grandparent function's Y
            alert(Y);
            // Knows about the greatgrandparent's X
            alert(X);
        }
        innermost();
    }
    inner();
}
outermost();

This seems simple enough, even expected. Now heres the trick, lets return some function pointers,

var X = "window";

function outermost() {
    var Y = "outermost";

    function inner() {
        // Knows about the parent function's Y
        alert(Y);

        function innermost() {
            // Knows about the grandparent function's Y
            alert(Y);
            // Knows about the greatgrandparent's X
            alert(X);
        }

        // 'inner' returns a reference to the 'innermost' function
        return innermost;
    }

    // 'outermost' returns a reference to the 'inner' function
    return inner;
}

Now lets call these functions,

// Get the reference to the 'inner' function
var inr = outermost();

// Call the 'inner' function
inr();  // alerts 'outermost'

// Get the reference to the innermost function
var inmost = inr(); // alerts 'outermost'

// Call the 'innermost' function
inmost();   // alerts 'outermost' and 'window'

As you can see, when we call the innermost function, using the reference returned by the call to inner, it still has access to the X and Y values as they were in scope when the innermost function was defined. This is Javascripts closure capturing information at the point of definition, so that the information can be used at the point of execution. Also the variables are not copies they are references.

A very common problem that occurs is in the unintended creation of closure,

for (var i = 0; i < 5; i++) {
    // This creates a closure!
    elements[i].onclick = function() { alert(i); };
}

Now clicking on any element will alert 4, why? Because all of the functions we created have a reference (not a copy) to the outer var i, probably not the effect you want. To fix this,

for (var i = 0; i < 5; i++) {
    // Don't create a function - no closure
    elements[i].onclick = Handle(i);
}

function Handle(i) {
    return function() { alert(i); };
}

You can read up more about closures here.

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