简体   繁体   English

javascript每次都评估包含数组文字的表达式

[英]javascript evaluate the expression containing the array literal each time

If an array is created using a literal in a top-level script, JavaScript interprets the array each time it evaluates the expression containing the array literal. 如果使用顶级脚本中的文字创建数组,则JavaScript在每次评估包含数组文字的表达式时都会解释该数组。

Source: MDN, "Values, variables, and literals" 来源: MDN,“值,变量和文字”

I can't understand it well. 我不太了解。
Someone can give me a example detailly. 有人可以给我一个例子。

Without knowing more context, it's hard to say what the author meant. 在不了解更多上下文的情况下,很难说出作者的意思。 (I checked, that text isn't from the spec.) Okay, now we know the source of the quote, I've checked, and the below applies. (我检查过,该文本不是来自规范。)好的,现在我们知道了报价的来源,我已经检查过了,以下内容适用。

Say you have this script: 说你有这个脚本:

var a = [1, 2, 3];

Every time that script is evaluated, that array initialiser ("literal") is evaluated. 每次评估脚本时,都会评估该数组初始化程序(“文字”)。 The array isn't created once and cached. 该数组不会创建一次并进行缓存。 In browser applications, it's rare to re-evaluate the top-level script without reloading the entire environment, but it's possible to do it, and if you do a new array is created each time. 在浏览器应用程序中,很少需要在不重新加载整个环境的情况下重新评估顶级脚本,但是可以这样做,并且每次创建一个新数组都可以。

I don't know why the quote would say "...in a top-level script..." as this is true anywhere. 我不知道为什么引号会说“ ...在顶级脚本中...”,因为在任何地方都是如此。 For instance: 例如:

function foo() {
    var a = [1, 2, 3];

    // ...
}

Every call to foo results in a new array. 每次对foo调用都会产生一个数组。 The expression, like all expressions, is evaluated every time it is encountered. 像所有表达式一样,每次遇到表达式时都会对其求值。 (There was a bug in the ES3 spec in this regard around regular expression literals, but it was fixed in ES5.) (在这方面,ES3规范中存在一个围绕正则表达式文字的错误,但已在ES5中修复。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为数组中的每个项目求值包含settimeout的函数(JavaScript) - How to evaluate a function containing settimeout for each item in an array (Javascript) Webpack:可以在编译时评估javascript表达式吗? - Webpack: Is it possible to evaluate javascript expression at compile time? 评估 Javascript 中的表达式树 - Evaluate expression tree in Javascript 不完整的JavaScript在执行时不会重新评估EL表达式 - Oncomplete JavaScript does not re-evaluate EL expression at time of execution 包含JavaScript中字符串文字的字符串 - String containing the literal of a string in JavaScript 如何正确评估包含带特殊字符的JSON.parse表达式的Javascript字符串? - How to correctly evaluate Javascript string containing JSON.parse expression with special characters? 从数组构建布尔表达式并在javascript中一次评估整个表达式 - Build boolean expression from arrays and evaluate entire expression at one time in javascript 使用 JavaScript 每次单击按钮时显示数组中的 10 个数字的数学表达式 - Mathematical expression to show 10 numbers from an array each time you click the button using JavaScript 遍历数组并计算表达式 - Loop through array and evaluate expression 在Javascript中是一个数组文字对象? - In Javascript is an array literal an object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM