WITH(this){a; b; c} [英] WITH (this) {a;b;c}

查看:135
本文介绍了WITH(this){a; b; c}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WITH(this){

a = 5

}


我发现this.a = 5将来自这个表达。我会

认为搜索a会出现未定义并留下




JavaScript是否明确关于在

WITH(对象)上创建变量的行为?

解决方案



SamJs写道:


JavaScript是否明确表示在

WITH(对象)上创建变量的行为?



这与with()构造无关。如果为

不存在的对象属性赋值,则引擎首先创建新属性,然后分配给它。


" SamJs" < Sa ******* @ gmail.comwrote:


WITH(this){

a = 5

}


我发现this.a = 5将来自这个表达式。我会

认为搜索a会出现未定义并留下




有多奇怪,我原以为你会从这个

表达式中得到语法错误。 Javascript没有''WITH''语句(虽然它确实有
有一个''with''语句)。


>

JavaScript是否明确表示在

WITH(对象)上创建变量的行为?



尝试这个,你会发现分配给''a''并没有创建一个

属性:


f = function(){

with(this){

a = 42;

}

}

var g = new f();

alert(" ga:" + g.a +",window.a:" + window.a);

//显示:ga:undefined,window.a:42

但以下代码将更新''this''的属性:


window.a = undefined;

f = function(){

this.a =''hello'';

with this this {

a = 42;

}

}

var g = new f();

提醒(ga: ; + g.a +",window.a:" + window.a);

//显示:ga:42,window.a:undefined


SamJs写道:


WITH(this){

a = 5

}


我发现this.a = 5将来自这个表达式。



语法错误除外,该观察取决于此

运算符的值。这个操作符可能并且经常会引用全局对象。


只要在范围链中没有变量a,

上面的赋值表达式将在全局

对象上创建一个新属性。如果这个操作符确实引用了那个对象,那么

表达式


this.a


将评估数字5.


比较:


var object = {

方法:function() {

with(this){

a = 5;

}

alert(this.a); < br $>
}

};


object.method();


这里, this运算符指的是赋给变量的对象,

对象。和以前一样,with语句

中的赋值表达式在全局对象上创建一个属性,但由于this运算符

值不同,表达式的计算结果为undefined。 br />

[snip]


JavaScript明确表示在

WITH上创建变量的行为(对象)?



是的:它永远不会发生。只能读取或分配现有属性。


Mike


WITH (this) {
a = 5
}

I''m finding that this.a = 5 will come from this expression. I would
have thought that the search for a would come up undefined and leave it
at that.

Is JavaScript explicit about the behavior of creating variables on a
WITH (object) ?

解决方案


SamJs wrote:

Is JavaScript explicit about the behavior of creating variables on a
WITH (object) ?

That is irrelevant to with() construct. If you assign a value to a
non-existing object property, the engine creates new property first and
then assign to it.


"SamJs" <Sa*******@gmail.comwrote:

WITH (this) {
a = 5
}

I''m finding that this.a = 5 will come from this expression. I would
have thought that the search for a would come up undefined and leave it
at that.

How strange, I would have thought you would get a syntax error from this
expression. Javascript doesn''t have a ''WITH'' statement (although it does
have a ''with'' statement).

>
Is JavaScript explicit about the behavior of creating variables on a
WITH (object) ?

Try this and you will find that assigning to ''a'' doesn''t create an
attribute on this:

f = function() {
with(this) {
a = 42;
}
}
var g = new f();
alert("g.a:"+g.a+", window.a:"+window.a);
// displays: g.a:undefined, window.a:42
but the following code will update the attribute on ''this'':

window.a = undefined;
f = function() {
this.a = ''hello'';
with(this) {
a = 42;
}
}
var g = new f();
alert("g.a:"+g.a+", window.a:"+window.a);
// displays: g.a:42, window.a:undefined


SamJs wrote:

WITH (this) {
a = 5
}

I''m finding that this.a = 5 will come from this expression.

Syntax error aside, that observation depends on the value of the this
operator. The this operator may, and often does, refer to the global object.

As long as there is no variable, a, within the scope chain, the
assignment expression above will create a new property on the global
object. If the this operator does indeed refer to that object, then the
expression

this.a

will evaluate to the number 5.

Compare to:

var object = {
method : function() {
with (this) {
a = 5;
}
alert(this.a);
}
};

object.method();

Here, the this operator refers to the object assigned to the variable,
object. As before, the assignment expression within the with statement
creates a property on the global object, but because the this operator
value is different, the expression evaluates to undefined.

[snip]

Is JavaScript explicit about the behavior of creating variables on a
WITH (object) ?

Yes: it never happens. Only existing properties can be read or assigned.

Mike


这篇关于WITH(this){a; b; c}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆