gpt4 book ai didi

javascript - Javascript 中这些函数被称为什么

转载 作者:行者123 更新时间:2023-12-02 15:29:55 25 4
gpt4 key购买 nike

(function createTaskRecord() {
// with some details
}()) // <--- whats the use of parentheses after the } ?

我是 Javascript 新手,希望了解 () 的函数是什么到底叫什么?为什么使用它们以及何时使用它们?

最佳答案

这称为立即执行。这将立即评估您的功能。

实际上,用这种方式定义的函数有一个更正式的名称:IIFE(立即调用函数表达式)。

Wikipedia有一篇关于它们的精彩文章:

An immediately-invoked function expression (or IIFE, pronounced "iffy") is a JavaScript design pattern which produces a lexical scope using JavaScript's function scoping. Immediately-invoked function expressions can be used to avoid variable hoisting from within blocks, protect against polluting the global environment and simultaneously allow public access to methods while retaining privacy for variables defined within the function. This pattern has been referred to as a self-executing anonymous function, but Ben Alman introduced the term IIFE as a more semantically accurate term for the pattern, shortly after its discussion arose on comp.lang.javascript.

关于javascript - Javascript 中这些函数被称为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33399357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com