“与”是什么意思?用 JavaScript 做什么?

发布于 2024-08-27 03:31:41 字数 146 浏览 5 评论 0原文

我看到以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

醉生梦死 2024-09-03 03:31:41

它增加了块中包含的语句的范围:

return sObj.options[selectedIndex].value;

可以成为:

with (sObj)
    return options[selectedIndex].value;

在您的情况下,它并没有做很多事情...但请考虑以下事项:

var a, x, y;
var r = 10;
a = Math.PI * r * r;
x = r * Math.cos(PI);
y = r * Math.sin(PI /2);

成为:

var a, x, y;
var r = 10;
with (Math) {
  a = PI * r * r;
  x = r * cos(PI);
  y = r * sin(PI / 2);
}

...节省了几次击键。 Mozilla 文档实际上做得很好,更详细地解释了一些事情(以及使用它的优点和缺点):

with - Mozilla 开发者中心

It adds to the scope of the statements contained in the block:

return sObj.options[selectedIndex].value;

can become:

with (sObj)
    return options[selectedIndex].value;

In your case, it doens't do a whole lot...but consider the following:

var a, x, y;
var r = 10;
a = Math.PI * r * r;
x = r * Math.cos(PI);
y = r * Math.sin(PI /2);

Becomes:

var a, x, y;
var r = 10;
with (Math) {
  a = PI * r * r;
  x = r * cos(PI);
  y = r * sin(PI / 2);
}

...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

莫言歌 2024-09-03 03:31:41

with 语句是纯粹的语法糖,但它也会导致一些令人讨厌的错误。

请参阅声明有害进行说明:

如果您无法阅读程序并确信自己知道它要做什么,那么您就无法确信它会正常工作。因此,应避免使用 with 语句。

the with statement is pure syntactical sugar, but it also can cause some nasty bugs.

See with Statement Considered Harmful for clarification:

If you can't read a program and be confident that you know what it is going to do, you can’t have confidence that it is going to work correctly. For this reason, the with statement should be avoided.

谁许谁一生繁华 2024-09-03 03:31:41

在 with 块中,您不必输入:

sObj.options[selectedIndex].value

但您可以使用:

options[selectedIndex].value

In that with block you dont have to type:

sObj.options[selectedIndex].value

but you can just use:

options[selectedIndex].value
中二柚 2024-09-03 03:31:41

它相当于

return sObj.options[selectedIndex].value;

With,允许您在特定对象的上下文中发出语句块。因此,with 块中的所有语句都被视为括号中对象的成员。

这有时可以使代码更具可读性,但是它也可能导致歧义,因为变量引用可以是 sObj 也可以是全局的。

javascript“with”语句的合法使用< /强>:D

Its the equivalent of

return sObj.options[selectedIndex].value;

With lets you issue a block of statements in the context of a particular object. Therefore all of the statements in the with 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

仄言 2024-09-03 03:31:41

由于性能问题,我建议不要使用它,但上面的意思是:

对于对象 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.

羁拥 2024-09-03 03:31:41

您的示例可以重写为...

return sObj.options[selectedIndex].value;

...因为“with”语句将所有相关语句置于所提供对象的范围内。在这种情况下,这是毫无意义的,但是,如果您在“sObj”上执行大量操作,那么它可以节省大量输入。

完全虚构的例子..

with (sObj) 
{
   if(options[selectedIndex].value < 10){
       options[selectedIndex].value++;
       total+ = options[selectedIndex].value;
   }
}

但是,话虽如此,通常情况下可以通过更好的方式来实现节省打字。

Your example could be rewritten as...

return sObj.options[selectedIndex].value;

...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..

with (sObj) 
{
   if(options[selectedIndex].value < 10){
       options[selectedIndex].value++;
       total+ = options[selectedIndex].value;
   }
}

But, having said that, it's often the case that saving typing can be achieved in better ways.

时光磨忆 2024-09-03 03:31:41

它不是一个函数(正如问题标题在编辑之前所指出的那样)而是一个语句。如果代码示例的格式如下,可能会更有意义:

with (sObj){
    return options[selectedIndex].value;
}

关于它的作用 ()

with 语句为一组语句建立默认对象。 JavaScript 查找语句集中的任何非限定名称,以确定这些名称是否是默认对象的属性。如果非限定名称与属性匹配,则在语句中使用该属性;否则,使用局部或全局变量。

这意味着在代码示例中,首先检查 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:

with (sObj){
    return options[selectedIndex].value;
}

Regarding what it does (Source)

The with statement establishes the default object for a set of statements. JavaScript looks up any unqualified names within the set of statements to determine if the names are properties of the default object. If an unqualified name matches a property, then the property is used in the statement; otherwise, a local or global variable is used.

Which means that in the code sample, it is first checked if options is a property of sObj. If it is then options refers to sObj.options, otherwise it checks other scopes for a variable defined by the name options

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文