google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Certains HTML simple JavaScript Exemple Codes sur la fonction Champ d\'application Dans ce JavaScript HTML tutorial article, Mark vous montre quelques codes JavaScript exemple simple pour en apprendre davantage sur la portée de fonction JavaScript. S'il vous plaît aller à la page full post pour plus de détails.


�tiquette: fonction JavaScript, champ d'application en fonction, HTML JavaScript tutorial

Gratuit iPage h�bergement Web pour la premi�re ann�e MOMENT



Si vous �tes toujours � la recherche d'un fournisseur d'h�bergement Web fiable avec des tarifs abordables, pourquoi vous ne prenez pas un peu de temps pour essayer iPage, seulement avec $1.89/month, inclus $500+ Cr�dits suppl�mentaires gratuites pour le paiement de 24 mois ($45)?

Plus de 1.000.000 de clients + existisng peuvent pas avoir tort, vraiment vous n'�tes pas aussi! Plus important encore, lorsque vous enregistrez l'h�bergement web � iPage gr�ce � notre lien, nous allons �tre heureux de renvoyer un plein remboursement. C'est g�nial! Vous devriez essayer iPage h�bergement web GRATUITEMENT maintenant! Et contactez-nous pour tout ce que vous devez savoir sur iPage.
Essayez iPage GRATUIT premi�re ann�e MOMENT

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!

AIVideo-App.com
Générez vos vidéos d'entreprise par l'IA avec la voix ou simplement du texte

chatGPTaz.com
Parlez à ChatGPT dans votre langue maternelle

AppAIVidéo
Votre première application vidéo AI GRATUITE

Deepfake Video
Deepfake AI Video Maker

Deepfake
Deepfake AI Video Maker

AI Deep Fake
Deepfake AI Video Maker

AIvidio
AI Video Mobile Solutions

AIvideos
AI Video Platform & Solutions

AIvedio
AI Video App Maker

Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT

Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT

Temu gratuit 500 $ pour les nouveaux utilisateurs
Claim Free Temu $500 Credit via Affiliate & Influencer Program

Crédits publicitaires TikTok gratuits
Maîtrisez les publicités TikTok pour le marketing de votre entreprise

Dall-E-OpenAI.com
Générez automatiquement des images créatives avec l'IA

chatGPT4.win
Parlez à ChatGPT dans votre langue maternelle

Premier produit d'intelligence artificielle d'Elon Musk - Grok/UN.com
Parlez au chatbot Grok AI dans votre langue

Outily.win
Centre d'outils ouvert et gratuit, utilisable par tous et pour tous, avec des centaines d'outils

GateIO.gomymobi.com
Airdrops gratuits à réclamer et à partager jusqu'à 150 000 $ par projet

iPhoneKer.com
Économisez jusqu'à 630 $ à l'achat d'un nouvel iPhone 16

Acheter le robot Tesla Optimus
Commandez votre robot Tesla Bot : Optimus Gen 2 dès aujourd'hui pour moins de 20 000 $

JavaScript par jour


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web