2017-06-08 4 views
0

Das letzte Beispiel von http://javascriptissexy.com/understand-javascript-closures-with-ease/ ist:Verständnis Beispiele aus Verstehen Sie JavaScript Closures mit Leichtigkeit

function celebrityIDCreator (theCelebrities) { 
    var i; 
    var uniqueID = 100; 
    for (i = 0; i < theCelebrities.length; i++) { 
     theCelebrities[i]["id"] = function (j) { // the j parametric variable is the i passed in on invocation of this IIFE​ 
      return function() { 
       return uniqueID + j; // each iteration of the for loop passes the current value of i into this IIFE and it saves the correct value to the array​ 
      }() // BY adding() at the end of this function, we are executing it immediately and returning just the value of uniqueID + j, instead of returning a function.​ 
     } (i); // immediately invoke the function passing the i variable as a parameter​ 
    } 
    return theCelebrities; 
}; 

​var actionCelebs = [{name:"Stallone", id:0}, {name:"Cruise", id:0}, {name:"Willis", id:0}]; 

​var createIdForActionCelebs = celebrityIDCreator (actionCelebs); 

​var stalloneID = createIdForActionCelebs[0]; 

console.log(stalloneID.id); // 100​ 

console.log(createIdForActionCelebs[1].id); // 101 

Ich verstehe nicht, warum IIFE hier gebraucht wird, ich ersetzt celebrityIDCreator mit und erzeugt die gleichen Ergebnisse:

var celebrityIDCreator = function(theCelebrities) { 
     var i; 
     var uniqueID = 100; 
     for (i = 0; i < theCelebrities.length; i++) { 
      theCelebrities[i]["id"] = function (j) { 
      return uniqueID + j; 
       // return function() { 

       // }() ; 
      } (i); 
     } 

     return theCelebrities; 
}; 

Könnte jemand das erklären? Vermisse ich etwas?

+1

Und keiner dieser Spielereien sind überhaupt notwendig, wenn Sie nur schreiben "für (lassen Sie mich!". Vielleicht ist es Zeit, ein aktuelleres Buch zu finden. –

Antwort

0

Ich denke, es ist ein Fehler auf dem Blog. Wenn Sie das erste Beispiel des Abschnitts Closures Gone Awry sehen, wird id als eine Funktion erstellt. Aber im zweiten Beispiel, auf das Sie sich beziehen und das darauf abzielt, einen Fehler in der ersten zu lösen, wird id als Eigenschaft erstellt.

Ich denke, das zweite Beispiel sollte so sein, um es ähnlich dem ersten zu machen. Beachten Sie die Hinweise im Code, in der Nähe des IIFE in Frage und dem Verbrauch von id als Funktion

function celebrityIDCreator(theCelebrities) { 
 
    var i; 
 
    var uniqueID = 100; 
 
    for (i = 0; i < theCelebrities.length; i++) { 
 
    theCelebrities[i]["id"] = function(j) { // the j parametric variable is the i passed in on invocation of this IIFE 
 
     return function() { 
 
     return uniqueID + j; // each iteration of the for loop passes the current value of i into this IIFE and it saves the correct value to the array 
 
     } // NOTE. Not an IIFE 
 
    }(i); // immediately invoke the function passing the i variable as a parameter 
 
    } 
 
    return theCelebrities; 
 
}; 
 

 

 
var actionCelebs = [{ 
 
    name: "Stallone", 
 
    id: 0 
 
}, { 
 
    name: "Cruise", 
 
    id: 0 
 
}, { 
 
    name: "Willis", 
 
    id: 0 
 
}]; 
 

 

 
var createIdForActionCelebs = celebrityIDCreator(actionCelebs); 
 

 

 
var stalloneID = createIdForActionCelebs[0]; 
 

 
console.log(stalloneID.id()); // 100 NOTE: Use as function 
 

 
console.log(createIdForActionCelebs[1].id()); // 101 NOTE: Use as function

Abgesehen davon, ich glaube, du bist nichts fehlt. Ihr Code ist richtig, wenn id eine Eigenschaft sein muss.