JavaScript的“with with”是否合法用途?声明? [英] Are there legitimate uses for JavaScript's "with" statement?
问题描述
Alan Storm的评论以回应我对 with
statement 让我思考。我很少找到使用这种特定语言功能的理由,并且从未考虑过如何引起麻烦。现在,我很好奇如何有效地使用和
,同时避免陷阱。
你在哪里找到和
语句有用吗?
发生了另一种用途今天对我来说,所以我兴奋地搜索了网络,发现现在已经提到它了:在块范围内定义变量。
背景
<尽管JavaScript与C和C ++表面相似,但它并没有将变量范围限定在它们定义的块中:
var name =Joe;
if(true)
{
var name =Jack;
}
//名称现在包含Jack
声明关闭循环是一个常见的任务,这可能导致错误:
for(var i = 0; i< 3; ++ i)
{
var num = i;
setTimeout(function(){alert(num);},10);
}
因为for循环没有引入新范围,所以 num
- 值为 2
- 将由所有三个函数共享。
新范围:让
和与
在 let 语句-US / docs / Web / JavaScript / Reference / Statements / letrel =noreferrer> ES6 ,在必要时引入新范围以避免这些问题变得容易:
//在此语句中引入的变量
//的范围限定为循环的每次迭代
for(let i = 0; i< 3; ++ i)
{
setTimeout(function(){alert(i);},10);
}
甚至:
for(var i = 0; i< 3; ++ i)
{
//此语句中引入的变量
//是作用域的到包含它的块。
let num = i;
setTimeout(function(){alert(num);},10);
}
在ES6普遍可用之前,此用途仅限于最新的浏览器和开发人员愿意使用转运。但是,我们可以使用与
轻松模拟此行为:
for( var i = 0; i< 3; ++ i)
{
//在此语句中引入的对象成员
//作用于其后的块。
with({num:i})
{
setTimeout(function(){alert(num);},10);
}
}
循环现在按预期工作,创建三个单独的变量值为0到2.请注意,在块中声明的变量不限于它,与C ++中块的行为不同(在C中,变量必须在块的开头声明,所以在某种程度上它是相似的)。这种行为实际上非常类似于 <$ c在早期版本的Mozilla浏览器中引入了$ c> let 块语法,但在其他地方没有广泛采用。
Alan Storm's comments in response to my answer regarding the with
statement got me thinking. I've seldom found a reason to use this particular language feature, and had never given much thought to how it might cause trouble. Now, I'm curious as to how I might make effective use of with
, while avoiding its pitfalls.
Where have you found the with
statement useful?
Another use occurred to me today, so I searched the web excitedly and found an existing mention of it: Defining Variables inside Block Scope.
Background
JavaScript, in spite of its superficial resemblance to C and C++, does not scope variables to the block they are defined in:
var name = "Joe";
if ( true )
{
var name = "Jack";
}
// name now contains "Jack"
Declaring a closure in a loop is a common task where this can lead to errors:
for (var i=0; i<3; ++i)
{
var num = i;
setTimeout(function() { alert(num); }, 10);
}
Because the for loop does not introduce a new scope, the same num
- with a value of 2
- will be shared by all three functions.
A new scope: let
and with
With the introduction of the let
statement in ES6, it becomes easy to introduce a new scope when necessary to avoid these problems:
// variables introduced in this statement
// are scoped to each iteration of the loop
for (let i=0; i<3; ++i)
{
setTimeout(function() { alert(i); }, 10);
}
Or even:
for (var i=0; i<3; ++i)
{
// variables introduced in this statement
// are scoped to the block containing it.
let num = i;
setTimeout(function() { alert(num); }, 10);
}
Until ES6 is universally available, this use remains limited to the newest browsers and developers willing to use transpilers. However, we can easily simulate this behavior using with
:
for (var i=0; i<3; ++i)
{
// object members introduced in this statement
// are scoped to the block following it.
with ({num: i})
{
setTimeout(function() { alert(num); }, 10);
}
}
The loop now works as intended, creating three separate variables with values from 0 to 2. Note that variables declared within the block are not scoped to it, unlike the behavior of blocks in C++ (in C, variables must be declared at the start of a block, so in a way it is similar). This behavior is actually quite similar to a let
block syntax introduced in earlier versions of Mozilla browsers, but not widely adopted elsewhere.
这篇关于JavaScript的“with with”是否合法用途?声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!