google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Vài ví dụ JavaScript về tầm vực Trong bài viết hướng dẫn sử dụng JavaScript này, Mark sẽ liệt kê vài ví dụ JavaScript đơn giản để bạn có thể hiểu hơn về tầm vực của hàm trong ngôn ngữ lập trình JavaScript.


Nhãn: ví dụ JavaScript, tầm vực, hàm JavaScript, lập trình 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

My colleague John Hume wrote an interesting post about his experience with the 'const' keyword in ActionScript where he describes the problems with trying to capture a loop variable in a closure and then evaluating it later on in the code.

Since ActionScript and JavaScript are both dialects of ECMAscript, this is a problem in JavaScript as well, and is due to the fact that variables in JavaScript have function scope rather than block scope which is the case in many other languages.

This problem would tend to reveal itself in code where we try to capture a loop variable in an anonymous function and use it later on, like so:

function getValues() {
    var x = new Array();

    for(var i=0; i < 10; i++) {

       x[i] = function() { return i; }

    }
    return x;
};
 
var values = getValues();

for(var j=0; j < values.length; j++) {

    console.log(values[j]());
}

We might expect that to print the sequence of numbers 0-9 on the screen but what we actually get is '10′ printed 10 times.

There are a couple of things that I initially found strange about this:

  1. Why doesn't it print out the numbers 0-9?
  2. Given that it doesn't do that why does it print out '10′ 10 times instead of '9′ 10 times?

The answer to the first question is that 'i' gets assigned a new value on each iteration of the loop and we don't evaluate 'i' until we evaluate the anonymous function on line 11.

The value when we do evaluate it would be the last value that it was set to by the loop which in this case that would be '10′ because that's the value that 'i' has to be in order for the loop to terminate.

This is actually a problem in C# as well - the following code will output '10′ 10 times as well:

[Test]
public void ClosureOnTheSameValue()
{
    var values = new List<Func<int>>();

    for(int i=0; i < 10; i++)

    {
        values.Add(() => i);
    }

 
    foreach (var value in values)
    {
        Console.WriteLine(value());

    }
}

Again we capture 'i' inside a closure and since we only evaluate that value when it's actually used it will always refer to the last value that 'i' was set to which in this case means that it will always output a value of 10.

To fix this in C# we could just create a temporary variable - something which Resharper will actually suggest to us:

[Test]
public void ClosureOnDifferentValue()

{
    var values = new List<Func<int>>();
    for(int i=0; i < 10; i++)

    {
        var idash = i;
        values.Add(() => idash);

    }
 
    foreach (var value in values)
    {

        Console.WriteLine(value());
    }
}

This works in C# because variables have block scope which means that we have a new version of 'idash' for each of the functions that we add to the 'values' collection.

Sadly the same trick doesn't work in JavaScript because variables have function scope in Javascript:

function getValues() {
    var x = new Array();

    for(var i=0; i < 10; i++) {

       var idash = i;
       x[i] = function() { return idash; }

    }
    return x;
};
 
var values = getValues();

for(var j=0; j < values.length; j++) {

    console.log(values[j]());
}

The 'idash' temporary variable that we created to try and solve the problem gets assigned a new value in each iteration of the loop because that variable is only declared once for the whole function.

The code above could be written like this to make that clearer:

function getValues() {
    var x = new Array();

    var idash;
 
    for(var i=0; i < 10; i++) {

       idash = i;
       x[i] = function() { return idash; }

    }
    return x;
};
 
var values = getValues();

for(var j=0; j < values.length; j++) {

    console.log(values[j]());
}

As John points out:

Here's something I either never knew or at some point forgot about JavaScript: variables are lexically scoped, but only function bodies introduce new lexical scopes.

In this case we actually end up printing '9′ 10 times because that's the maximum value that gets assigned to 'idash'.

One solution is to create a temporary variable inside an anonymous function that we execute immediately, like this:

function getValues() {
    var x = new Array();

    for(var i=0; i < 10; i++) {

        (function() {
            var idash = i;

            x[i] = function() { return idash; } })();

    }
    return x;
};
 
var values = getValues();

for(var j=0; j < values.length; j++) {

    console.log(values[j]());
}

Now 'idash' is scoped inside the anonymous function and we therefore end up with a new value each time like we want.

Raph pointed out that we could achieve the same thing in a simpler way with the following code:

function getValues() {
    var x = new Array();

    for(var i=0; i < 10; i++) (function(i) {

        x[i] = function() { return i; };

    })(i);
    return x;
};

 
var values = getValues();
for(var j=0; j < values.length; j++) {

    console.log(values[j]());
}

Here we define a for loop with just a single statement so we can lose the '{}' and just call an anonymous function passing in 'i'.

Of course this example is truly contrived but I wanted to pick something simple enough that I could try and follow exactly how it worked.

I'm not entirely sure of the terminology around closures and scoping so if I've described anything incorrectly then please correct me!

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