javascript - 为什么当我尝试使用 IFFE 时这段代码不起作用
问题描述
我试图运行这段代码。
var temp = (function () {
var a =10;
return {
function (a) {
console.log('Value of a is ' + a);
}
}
})();
temp();
我希望 IIFE 返回该函数并将其分配给 temp 变量,但相反,我收到以下错误。
Uncaught TypeError: temp is not a function
那有什么问题?
解决方案
您需要从返回中删除对象文字并返回一个命名函数
var temp = (function() {
var a = 10;
return function test(a) {
console.log('Value of a is ' + a);
}
})();
temp();
推荐阅读
- r - Extracting rows matching conditions based on previous rows - r
- json - 预计解码数组
而是找到了一本字典。” - javascript - 嵌套时如何修改CSS值?
- java - 如何使用 Java 解析 XML 响应中的属性
- javascript - 如何将返回传递的数据传递回组件以在 html 中显示
- python - 生产中的多标签二进制化器
- java - Log4j 版本 1 自定义 Throwable (stacktrace) 渲染器,用于删除换行符
- python - 确定两个字符串的排列
- python - 如何生成整数的 n 位组合?
- javascript - 如何从选择的 div 覆盖图像