“与”是什么意思?用 JavaScript 做什么?
我看到以 with
开头的 JavaScript 代码。这有点令人困惑。它有什么作用以及如何正确使用?
with (sObj) return options[selectedIndex].value;
I saw JavaScript code which begins with with
. That's a bit confusing. What does it do and how can it be used correctly?
with (sObj) return options[selectedIndex].value;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
它增加了块中包含的语句的范围:
可以成为:
在您的情况下,它并没有做很多事情...但请考虑以下事项:
成为:
...节省了几次击键。 Mozilla 文档实际上做得很好,更详细地解释了一些事情(以及使用它的优点和缺点):
with - Mozilla 开发者中心
It adds to the scope of the statements contained in the block:
can become:
In your case, it doens't do a whole lot...but consider the following:
Becomes:
...saves a couple of keystrokes. The Mozilla documentation actually does a pretty good job of explaining things in a little more detail (along with pros and cons of using it):
with - Mozilla Developer Center
with 语句是纯粹的语法糖,但它也会导致一些令人讨厌的错误。
请参阅声明有害进行说明:
the
with
statement is pure syntactical sugar, but it also can cause some nasty bugs.See with Statement Considered Harmful for clarification:
在 with 块中,您不必输入:
但您可以使用:
In that with block you dont have to type:
but you can just use:
它相当于
With
,允许您在特定对象的上下文中发出语句块。因此,with
块中的所有语句都被视为括号中对象的成员。这有时可以使代码更具可读性,但是它也可能导致歧义,因为变量引用可以是 sObj 也可以是全局的。
javascript“with”语句的合法使用< /强>:D
Its the equivalent of
With
lets you issue a block of statements in the context of a particular object. Therefore all of the statements in thewith
block are taken to be members of the object in parenthesis.This can make code more readable at times, but it also can lead to ambiguity, since the variable references can either be with sObj or global.
legitimate uses for javascript's "with" statement :D
由于性能问题,我建议不要使用它,但上面的意思是:
对于对象 sObj (这里可能是一个 select 元素),在此对象上引用的所有子项和属性(或在以下卷曲之间)大括号)将其视为其父范围。
I would recommend NOT using this because of performance issues, but what the above means is:
for the object sObj (here presumably a select element), all children and properties referenced on this one (or between following curly braces) treat that as their parent scope.
您的示例可以重写为...
...因为“with”语句将所有相关语句置于所提供对象的范围内。在这种情况下,这是毫无意义的,但是,如果您在“sObj”上执行大量操作,那么它可以节省大量输入。
完全虚构的例子..
但是,话虽如此,通常情况下可以通过更好的方式来实现节省打字。
Your example could be rewritten as...
...as the 'with' statement places all related statements in the scope of the supplied object. In this case, it's pretty pointless but, if you were doing lots of operations on 'sObj', then it saves a lot of typing.
Totally ficticious example..
But, having said that, it's often the case that saving typing can be achieved in better ways.
它不是一个函数(正如问题标题在编辑之前所指出的那样)而是一个语句。如果代码示例的格式如下,可能会更有意义:
关于它的作用 (源)
这意味着在代码示例中,首先检查
options
是否是sObj
的属性。如果是,则options
引用sObj.options
,否则它会检查由名称options
定义的变量的其他作用域。
with
语句的缺点是,仅看代码是不可能知道访问了什么内容的。还有其他更好的替代方案,如这篇文章< /a>It isn't a function (as was indicated in the question title before it was edited) but a statement. It may make more sense if the code sample is formatted like so:
Regarding what it does (Source)
Which means that in the code sample, it is first checked if
options
is a property ofsObj
. If it is thenoptions
refers tosObj.options
, otherwise it checks other scopes for a variable defined by the nameoptions
The downside of using a
with
statement is that it is impossible to know from just glancing at the code what gets accessed. There are other better alternatives as shown in this article它给你和他人带来痛苦
It brings pain and suffering among you and others