我目前对如何使代码IIFE自我调用感到困惑。问题陈述是:
您的客户希望从一个邮政编码研究(每个列表仅列出一次)中按从小到大的顺序列出一个邮政编码列表。他希望它“自动运行”(自动调用)。
我的代码显示正确的输出,所有邮政编码从最小到最大都列出一次。我需要帮助来了解如何使当前代码成为“自调用”代码。这是我当前的代码:
//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;
};
答案 0 :(得分:0)
您可以在首次加载时调用一次:
function assignment12_3() {
// Your code goes in here.
}
assignment12_3(); // invoke it once
或者:
(function assignment12_3() {
// Your code goes in here.
})()