javascript - 当前代码需要是 IIFE(自调用) - 代码工作并运行
问题描述
我目前对如何使我的代码 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;
};
解决方案
您可以在第一次加载时调用它:
function assignment12_3() {
// Your code goes in here.
}
assignment12_3(); // invoke it once
或者:
(function assignment12_3() {
// Your code goes in here.
})()
推荐阅读
- java - 一种从 JVM 内存中获取所有对象实例的方法
- node.js - Discord.js - 反应收集器的问题
- c# - 从 C# 程序查询 API 时的 HTTP 状态代码 500,但 API 有效
- github - 在 Github 拉取请求页面上显示 Travis-CI 构建状态
- php - Bootstrap 导航切换器未关闭 wordpress 中的菜单
- python - 在链中的 pandas 中添加组属性(类似于 dplyr group_by - R 中的变异)
- reactjs - 为 ReactJS 和 Asp.net 核心创建 Docker 镜像
- javascript - 在javascript中定义函数时,有没有办法将数组作为参数传递?
- sql - 如果其他列等于 SQL Oracle 中的特定值,则创建新列
- python - 一维卷积神经网络权重矩阵