当前代码需要是 IIFE(自调用)- 代码可以运行
Current code needs to be IIFE(Self-Invoked) - Code works and runs
我目前对如何使我的代码 IIFE、自调用感到困惑。问题陈述是:
您的客户想要一份来自邮政编码研究的邮政编码列表(每个只列出一次),按从小到大的顺序排列。他希望它"just run"(自调用)。
我的代码显示了正确的输出,其中所有邮政编码从小到大都列出了一次。我需要帮助来理解如何使我当前的代码成为 "self-invoking"。这是我当前的代码:
//Start.
window.onload = uniqueZipcodes;
function assignment12_3() {
// Your code goes in here.
}
function uniqueZipcodes(){
//Start.
//Variables
var records, zip;
var output = document.getElementById("selfInvokingFunctionDiv");
var zipcodes = [];
var outputString = "";
//Gets the records...
records = openZipCodeStudyRecordSet();
//This will loop through the records and put unique records
//into an array
while(records.readNextRecord()){
zip = records.getSampleZipCode();
if(!zipcodes.includes(zip)){
zipcodes.push(zip);
}
}
//Will sort the zipcodes
zipcodes.sort();
//outputs the zipcodes.
for(var z in zipcodes){
outputString += zipcodes[z] + "</br>";
}
outputDiv.innerHTML += outputString;
};
您可以在第一次加载时调用它:
function assignment12_3() {
// Your code goes in here.
}
assignment12_3(); // invoke it once
或者:
(function assignment12_3() {
// Your code goes in here.
})()
我目前对如何使我的代码 IIFE、自调用感到困惑。问题陈述是:
您的客户想要一份来自邮政编码研究的邮政编码列表(每个只列出一次),按从小到大的顺序排列。他希望它"just run"(自调用)。
我的代码显示了正确的输出,其中所有邮政编码从小到大都列出了一次。我需要帮助来理解如何使我当前的代码成为 "self-invoking"。这是我当前的代码:
//Start.
window.onload = uniqueZipcodes;
function assignment12_3() {
// Your code goes in here.
}
function uniqueZipcodes(){
//Start.
//Variables
var records, zip;
var output = document.getElementById("selfInvokingFunctionDiv");
var zipcodes = [];
var outputString = "";
//Gets the records...
records = openZipCodeStudyRecordSet();
//This will loop through the records and put unique records
//into an array
while(records.readNextRecord()){
zip = records.getSampleZipCode();
if(!zipcodes.includes(zip)){
zipcodes.push(zip);
}
}
//Will sort the zipcodes
zipcodes.sort();
//outputs the zipcodes.
for(var z in zipcodes){
outputString += zipcodes[z] + "</br>";
}
outputDiv.innerHTML += outputString;
};
您可以在第一次加载时调用它:
function assignment12_3() {
// Your code goes in here.
}
assignment12_3(); // invoke it once
或者:
(function assignment12_3() {
// Your code goes in here.
})()