
[英]JavaScript: Can ECMAScript 5's Strict Mode (“use strict”) be enabled using single quotes ('use strict')?
[英]Can I target multiple objects with a single operation using ECMAScript's `with` statement?
以下不起作用(尽管它没有给出明确的错误),但为什么不呢?
而且......真的没有办法绕过它,严格使用with 语句吗? 忘记使用 for / foreach 。
with (object1, object2) {
attribute = value;
method();
}
编辑:很抱歉在 1 中提出 2 个问题。我会尽量让它更清楚:
为什么上面的代码没有给出语法错误,不起作用但被with
接受?
如果可能,我们如何使用with
更改具有相同属性的多个对象?
希望下面的例子能更清楚地说明我想要完成的事情:
var object1 = { attribute: 3 };
var object2 = { attribute: 2, method: function() { alert('blah'); } };
var object3 = { method: function() {alert('bleh'); } };
var value = 4;
with (object1)
with (object2)
with (object3)
{
attribute = value;
method();
}
alert(object1.attribute + object2.attribute);
// resulting alerts should be, in order: blah, bleh, 8
with
这就是我最初认为你所追求的,因为你没有指定你的预期结果是什么。 只需堆叠with
语句:
var object1 = { attribute: 3 };
var object2 = { method: function() { alert('blah'); } };
var value = 4;
with (object1)
with (object2)
{
attribute = value;
method();
}
alert(object1.attribute);
自然,最内层with
引入的 object 将覆盖任何外层 scope 中的同名属性,包括那些外层with
语句。
标准免责声明适用于使用with
导致的性能损失和潜在错误。 请注意,您的示例显示了以 . 为前缀的属性访问.
在块内,但这是不正确with
- 修改 scope,暂时将 object 放在解析链的前面,因此不需要(或允许)前缀。
关于您的编辑: 逗号运算符允许您编写一些似乎将多个表达式传递给with
东西,但实际上只传递其中一个。 如果没有某种多播委托实现(涉及循环的实现),您将无法完成您想要的; JavaScript/ECMAScript 没有任何内置方法可以通过单个赋值/调用来修改多个属性/调用多个方法。
function multicast()
{
this.targets = Array.prototype.slice.call(arguments, 0);
}
multicast.prototype = {
call: function(methodName)
{
var i;
for (i=0; i<this.targets.length; ++i)
{
if ( this.targets[i][methodName] )
this.targets[i][methodName].apply(this.targets[i],
Array.prototype.slice.call(arguments, 1));
}
},
set: function(propName, value)
{
var i;
for (i=0; i<this.targets.length; ++i)
this.targets[i][propName] = value;
}
};
var object1 = { attribute: 3 };
var object2 = { attribute: 2, method: function() { alert('blah'); } };
var object3 = { method: function() {alert('bleh'); } };
var value = 4;
var delegate = new multicast(object1, object2, object3);
delegate.set('attribute', value);
delegate.call('method');
alert(object1.attribute + object2.attribute);
如果你想让你的代码更短,你也可以参考 object:
var o1 = object1
, o2 = object2
;
o1.attribute = value;
o2.method();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.