gpt4 book ai didi

javascript - 未绑定(bind)的 JavaScript 文字会发生什么?

转载 作者:数据小太阳 更新时间:2023-10-29 04:05:00 24 4
gpt4 key购买 nike

未绑定(bind)(也称为分配)到变量的 JavaScript 文字(字符串、数字)会怎样?

// A comment
"Practically, also a comment"
var assigned = "something"
53
423.0022
NaN
"does it impact performance"
// or is it treated just like a comment?

浏览器似乎忽略了它们,但我在 spec 中找不到特定规则

最佳答案

这些是“表达式语句”。此类表达式会被求值,但由于它们未被赋值,因此不会存储它们的值。 JavaScript 引擎很可能会检测到那些没有副作用的东西,并将它们消除,就好像它们从未存在过一样。

但其中至少有一个是有影响的:

 "use strict";

这有一个JavaScript directive的意思

来自EcmaScript specification :

A Use Strict Directive is an ExpressionStatement in a Directive Prologue whose StringLiteral is either the exact code unit sequences "use strict" or 'use strict'.

另请注意,其他字符串文字可能在指令序言中使用时具有特殊含义:

Implementations may define implementation specific meanings for ExpressionStatement productions which are not a Use Strict Directive and which occur in a Directive Prologue.

关于javascript - 未绑定(bind)的 JavaScript 文字会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44834144/

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