0

我目前对如何使我的代码 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;
};
4

1 回答 1

0

您可以在第一次加载时调用它:

function assignment12_3() {
    // Your code goes in here.
}

assignment12_3();  // invoke it once

或者:

(function assignment12_3() {
    // Your code goes in here.
})()
于 2019-10-03T01:29:57.127 回答