undefined vs. undefined(was:new Array()vs []) [英] undefined vs. undefined (was: new Array() vs [])

查看:85
本文介绍了undefined vs. undefined(was:new Array()vs [])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(请参阅原始帖子中的ASM帖子;可以在

< http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/看到3716384d8bfa1b0b>

作为选项)


因为这与new Array()vs []无关问题或者对于
数组的表现,我敢把它移到一个新的线程。


Gecko严格按照第4章第3章的规定获取未定义的值,歌曲9

ECMA书籍

:-)

(ECMAScript语言规范第4.3.9段,第3版)

: - |


< quote未定义的值是当变量

尚未赋值时使用的原始值。 < / quote>


作为一个复杂的问题JavaScript是我所知道的唯一语言

可以写的:

var foo = undefined;

这是 - 严格来说 - 保持foo但是给它赋值

foo未分配且从未存在过这是 - 甚至更严格地说

说话 - 完全没有意义。


如果在数组初始化程序中有一个省略,你*做*赋值给

每个阵列成员(Cornford先生会说类似

" [[Put]]方法为每个评估结果调用)。

这种方式

var arr = [''A'',''B'']

的确意味着

var arr = [''A'',undefined,''B'']

,在

初始化阶段,三个数组成员明确地分配给数组。并且每个*分配的*数组成员都是

,反映为底层Object

对象的可枚举对象属性。要看到差异(以及真正意义上的未定义值)

试试这个:


< script type =" text / javascript">

函数f(arg){

for(var p in arg){

window.alert(arg [p] ||''undefined '');

}

window.alert(''loops:''+ i);

}


函数demo(){

var arr = [''A'',''B''];

arr [100] = ''C'';

f(arr);

}


window.onload = demo;

< / script>

arr [1]成员获得明确的作业;因此,您将您的

数组视为对象类型,您将其视为属性1。有价值

undefined。

arr [100] ='''C''之后;你得到96个成员与下一个定义的

值的差距。但arr [3] ...... arr [99]从未参与任何任务

操作。它们是未定义的,仅存在于抽象数组中。

连续空间。因此它们不会被反映为对象属性。

解决方案

VK写道:


(参见ASM在原始帖子中的帖子;可以在



< http://groups.google.com/group/comp....e_frm/thread/3

716384d8bfa1b0b>


作为选项)


因为这不相关tonew Array()vs []"问题

或数组性能,我敢把它移到一个新线程。



或者是因为如果你发布

,如果没有上下文就可以避免看起来像傻瓜一样。


Gecko严格按照第4册第3章的要求获取未定义的值。

ECMA书籍第9页

:-)

(ECMAScript语言规范第4.3.9段,第3版)

: - |



ECMA 262中的条款编号与段落无关,

书籍,章节或歌曲。


< quote未定义的值是

a变量尚未赋值时使用的原始值。 < /报价>



10.1.3节解释了未定义值到

实例化变量的分配。并且没有_any_ ECMAScript

实现的可观察行为与10.1.3节相矛盾。


作为一个复杂问题JavaScript是我唯一的语言/>
知道可写的地方:

var foo = undefined;



该声明中javascript的唯一特定内容是使用 - var -

来声明一个变量(这可能不是唯一的javascript)。


这是 - 严格来说 - 保持foo但指定它没有分配并且从未存在的

值 ;



废话。该陈述的含义分为两部分: -


1.在执行上下文的''变量实例化'期间;在

执行上下文的激活/变量对象上创建名为''foo'的
a属性(带有DontDelete属性)并分配

未定义的值。


2.执行代码到达赋值表达式时:

评估左侧(参考参考文献)键入

激活/变量对象作为其基础属性和''foo''

作为其属性名称)。右侧评估为

值:

标识符''undefined''在范围链中解析

给出一个参考类型。 ECMAScript Edition 3引入了全局对象的

属性,名称为'undefined''设置为

未定义值,因此Reference类型可能具有

全局对象作为其基础属性。在ECMAScript之前的版本

3环境中没有全局''undefined''属性

作为扩展,或者没有程序员定义的替代方案已经

创建后,Reference类型的'base'属性将为null。


生成的Reference类型随后传递给内部

。GetValue函数用于恢复值(如果Reference类型具有null''base''

属性),此时将抛出异常




然后右侧的值与左侧的Reference

类型一起使用以分配值。名为''foo''的Activation / Variable对象的

属性是

分配右侧的值(假设没有异常

从标识符恢复值时被抛出

- undefined - )。


这是 - 更严格的是

说话 - 完全没有意义。



你所写的内容毫无意义,但这只是你理解javascript的一个表现。实际的陈述不仅仅是(并且不低于)评估变量(未定义)并将其值分配给另一个变量(foo)。

。 >


如果在数组初始化程序中有一个省略,你*为每个数组成员分配




垃圾。对elision的评估不会导致任何值被分配给数组的任何元素,除了它的长度 - 属性在

中没有遵循elision的事件通过AssignmentExpression。


(Cornford先生会说类似

" [[Put]]方法被调用每个评估结果" )。



我不太可能说出完全错误的东西。


这样

var arr = [''A'',''B'']

的确意味着

var arr = [''A'',undefined, ''B'']



不,不是。它更接近: -


var arr = new Array;

arr [0] =''A'';;

arr [2] =''B'';


,三个数组成员明确地分配给数组

初始化阶段。



如果确实发生了这将是一个实现错误(就像在另一个线程中讨论的firefox / Mozilla版本的

中的情况一样) )。


每个*分配的*数组成员都是

反映为
$ b的可枚举对象属性$ b底层Object对象。要查看差异(以及

真正意义上的未定义值),请尝试以下方法:


< script type =" text / javascript">

函数f(arg){

for(var p in arg){

window.alert(arg [p] ||''undefined '');

}

window.alert(''loops:''+ i);

}


函数demo(){

var arr = [''A'',''B''];

arr [100] = ''C'';

f(arr);

}


window.onload = demo;

< / script>

arr [1]成员获得明确的作业;



如果确实如此,那将是实现错误。


因此你将你的数组视为一个对象类型,



没有任何意义上任何人都可以将数组视为不是

是一个对象类型,因为它总是属于那种类型。


你认为它是属性1有价值

undefined。

arr [100] ='''C''之后;你得到96个成员的差距

下一个定义的值。但arr [3] ...... arr [99]从未在任何作业中参与过
。它们是未定义的并且仅在抽象数组连续空间中存在

。因此它们不会反映为对象属性。



所以,除了证明你无耻地发布材料的能力之外,这是100%的事实错误(也就是说,你的断言是关于什么的

ECMA 262 3rd Ed。说并且意味着与现实相反)什么是

你的观点?


至少其他线程得出(正确的)结论,描述的

firefox / Mozilla行为是一个实现错误。


Richard。




Richard Cornford写道:


至少另一个线程得出(正确的)结论

firefox / Mozilla所描述的行为是一个实现错误。



我肯定会在Bugzilla上澄清这一点并在此发布回复。

尽管如此,在JavaScript引擎行为方面,Mozilla仍然是

最有可能达到ECMAScript标准,至少从

开始到现在为止。


在Mozilla团队给出之前回答,我对

这个问题说了拙见:这是规范bug的另一个副作用;-)

允许未定义的值被*赋值*并参与一些

内部*作业*操作。


我很高兴我不需要做出如何对待房产的选择

,具有明确赋值的此属性不存在且从不

存在。

作为具有此类价值的财产? (Gecko)

作为不存在的财产? (IE和Co)


说实话,我想放弃一枚硬币,然后去瑞士

来打一些愚蠢的脑袋: - )


VK写道:


Richard Cornford写道:


>至少其他线程得出(正确的)结论
所描述的firefox / Mozilla行为是一个
实现错误。



我肯定会在Bugzilla上澄清这个并在这里发布

回复。



为什么,当你决定不应该添加你的帖子时,

包括bugzilla报告(确认它是一个错误)?


尽管如此,在JavaScript引擎行为方面

Mozilla最有可能通过ECMAScript标准,在

从开始到现在最少。



不是。很少有实际的实现错误,而且那些少量的b $ b $均匀分布。


在Mozilla团队给出答案之前,



太晚了。


我说这个问题的拙见:



没有人关心你的意见。


这是规范bug的另一个副作用;-)



半智能。


允许未定义的值被*分配*并且

涉及一些内部*任务*操作。



你现在已经很好地理解了javascript,但是

作为一个完整指定行为的一致逻辑系统(所以

是完全可以理解和可预测的)无能是仅仅反映在你的心理过程中而不是别的。


我很高兴我不需要做出选择如何

对待具有明确赋值的财产这个

财产不存在从来没有存在过。



这是一种无稽之谈,可以清楚说明为什么你的opi9nion没有价值。


作为具有这样价值的房产? (Gecko)

作为不存在的财产? (IE和Co)


说实话,我想放弃一枚硬币,然后转到瑞士的b
$:



规范是明确的,Mozilla / Gecko是(或是)

错误并将被修复以使其符合规范。


Richard。


(see the post by ASM in the original thread; can be seen at
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b>
as an option)

As that is not in relevance to "new Array() vs []" question or to the
array performance, I dared to move it to a new thread.

Gecko takes undefined value strictly as per Book 4, Chapter 3, Song 9
of Books of ECMA
:-)
(Paragraph 4.3.9 of ECMAScript Language Specification, 3rd edition)
:-|

<quoteThe undefined value is a primitive value used when a variable
has not been assigned a value. </quote>

As a matter complication JavaScript is the only language I know where
one can write:
var foo = undefined;
which is - strictly speaking - "keep foo but assign it the value that
foo was not assigned and never existed" which is - even more strictly
speaking - totally meaningless.

In case of an elision in an array initializer you *do* assign values to
each and every array member (Mr. Cornford would say something like
"[[Put]] method is called for each evaluation result").

This way
var arr = [''A'',,''B'']
really means
var arr = [''A'',undefined,''B'']
with three array members explicetly assigned to the array on the
initialization stage. And each and every *assigned* array member is
reflected as an enumerable object property of the underlaying Object
object. To see the difference (and the real sense of undefined value)
try this:

<script type="text/javascript">
function f(arg) {
for (var p in arg) {
window.alert(arg[p] || ''undefined'');
}
window.alert(''loops: '' + i);
}

function demo() {
var arr = [''A'',,''B''];
arr[100] = ''C'';
f(arr);
}

window.onload = demo;
</script>

arr[1] member gets an explicit assignment; thus then you treat your
array as an Object type, you see it as property "1" with value
undefined.
After arr[100] = ''C''; you''re getting 96 members gap to the next defined
value. But arr[3]...arr[99] never participated in any assignment
operations. They are undefined and existing only in the abstract array
continuum space. Therefore they are not reflected as object properties.

解决方案

VK wrote:

(see the post by ASM in the original thread; can be seen at

<http://groups.google.com/group/comp....e_frm/thread/3
716384d8bfa1b0b>

as an option)

As that is not in relevance to "new Array() vs []" question
or to the array performance, I dared to move it to a new thread.

Or was it because you might avoid looking as much of a fool if you post
this without a context as you would if you did.

Gecko takes undefined value strictly as per Book 4, Chapter 3,
Song 9 of Books of ECMA
:-)
(Paragraph 4.3.9 of ECMAScript Language Specification, 3rd edition)
:-|

The numbering of clauses in ECMA 262 has no relationship to paragraphs,
books, chapters or songs.

<quoteThe undefined value is a primitive value used when
a variable has not been assigned a value. </quote>

And section 10.1.3 explains the assignment of the undefined value to
instantiated variables. And no observable behaviour of _any_ ECMAScript
implementations contradicts section 10.1.3.

As a matter complication JavaScript is the only language I
know where one can write:
var foo = undefined;

The only thing specific to javascript in that statement is using - var -
to declare a variable (and that may not be unique to javascript).

which is - strictly speaking - "keep foo but assign it the
value that foo was not assigned and never existed"

Nonsense. The meaning of that statement is in two parts:-

1. During ''variable instantiation'' for the execution context; create
a property named ''foo'' on the Activation/Variable object for the
execution context (with a DontDelete attribute) and assign the
Undefined value to it.

2. When execution of the code arrives at the assignment expression:
The left-hand side is evaluated (into a Reference type with
the Activation/Variable object as its ''base'' property and ''foo''
as its property name). The right hand side is evaluated to a
value:
The Identifier ''undefined'' is resolved against the scope chain
to give a Reference type. ECMAScript Edition 3 introduced a
property of the global object with the name ''undefined'' set to
the Undefined value so the Reference type will likely have the
global object as its ''base'' property. In pre-ECMAScript edition
3 environments that do not have a global ''undefined'' property
as an extension, or no programmer defined alternative has been
created, the ''base'' property of the Reference type will be null.

The resulting Reference type is then passed to the internal
GetValue function to recover a value (and exception will be
thrown at this point if the Reference type has a null ''base''
property).

The value of the right hand side is then used with the Reference
type from the left hand side in order to assign the value. The
property of the Activation/Variable object named ''foo'' is
assigned the value of the right hand side (assuming no exception
was thrown while recovering the value from the Identifier
- undefined -).

which is - even more strictly
speaking - totally meaningless.

What you wrote is meaningless, but that is just a manifestation of your
not understanding javascript. The actual statement is doing nor more
than (and no less than) evaluating a variable (undefined) and assigning
its value to another variable (foo).

In case of an elision in an array initializer you *do* assign
values to each and every array member

Rubbish. The evaluation of an elision will not result in any value being
assigned to any element of an array except its - length - property in
the event that the elision is not followed by an AssignmentExpression.

(Mr. Cornford would say something like
"[[Put]] method is called for each evaluation result").

I am unlikely to say something that is utterly false.

This way
var arr = [''A'',,''B'']
really means
var arr = [''A'',undefined,''B'']

No it does not. It is closer to:-

var arr = new Array;
arr[0] = ''A'';;
arr[2] = ''B'';

with three array members explicetly assigned to the array
on the initialization stage.

If that did happen it would be an implementation bug (as was the case in
the firefox/Mozilla versions discussed in the other thread).

And each and every *assigned* array member is
reflected as an enumerable object property of the
underlaying Object object. To see the difference (and the
real sense of undefined value) try this:

<script type="text/javascript">
function f(arg) {
for (var p in arg) {
window.alert(arg[p] || ''undefined'');
}
window.alert(''loops: '' + i);
}

function demo() {
var arr = [''A'',,''B''];
arr[100] = ''C'';
f(arr);
}

window.onload = demo;
</script>

arr[1] member gets an explicit assignment;

If it does then that would be in implementation bug.

thus then you treat your array as an Object type,

There is no meaningful sense in which anyone could treat an array as not
being an object type, as it always is of that type.

you see it as property "1" with value
undefined.
After arr[100] = ''C''; you''re getting 96 members gap to
the next defined value. But arr[3]...arr[99] never participated
in any assignment operations. They are undefined and existing
only in the abstract array continuum space. Therefore they are
not reflected as object properties.

So, apart from demonstrating your ability to shamelessly post material
that is 100% factually false (that is; your assertions here about what
ECMA 262 3rd Ed. says and means are the opposite of reality) what is
your point?

At lest the other thread came to the (correct) conclusion that the
firefox/Mozilla behaviour described was an implementation bug.

Richard.



Richard Cornford wrote:

At lest the other thread came to the (correct) conclusion that the
firefox/Mozilla behaviour described was an implementation bug.

I will definitely clarify this on Bugzilla and post the response here.
Nevertheless in the matter of JavaScript engine behavior Mozilla is
most likely to be by ECMAScript standard, at least it was since the
beginning and until now.

Before Mozilla team will give an answer, I say my humble opinion on
this matter: it is another side effect of specification bug ;-)
allowing undefined value to be *assigned* and being involved in some
internal *assignment* operations.

I''m glad that I don''t need to make a choice how to treat a property
with explicetly assigned value "this property doesn''t exist and never
existed".
As a property with such value? (Gecko)
As non-existing property? (IE and Co)

Truthfully I would feel like to drop a coin, and then go to Switzerland
to hit some dumb heads :-)


VK wrote:

Richard Cornford wrote:

>At lest the other thread came to the (correct) conclusion
that the firefox/Mozilla behaviour described was an
implementation bug.


I will definitely clarify this on Bugzilla and post the
response here.

Why, when the tread you decided your post should not be added to already
includes the bugzilla report (with its confirmation that it was a bug)?

Nevertheless in the matter of JavaScript engine behavior
Mozilla is most likely to be by ECMAScript standard, at
least it was since the beginning and until now.

Not really. There are very few actual implementation bugs and those few
are pretty evenly distributed.

Before Mozilla team will give an answer,

Too late then.

I say my humble opinion on this matter:

Nobody cares what your opinion is.

it is another side effect of specification bug ;-)

Half-wit.

allowing undefined value to be *assigned* and being
involved in some internal *assignment* operations.

Your inability to understand javascript is well established by now, but
as a consistent, logical system with its behaviour fully specified (so
completely understandable and predictable) that inability is a
reflection only on your mental processes and nothing else.

I''m glad that I don''t need to make a choice how to
treat a property with explicetly assigned value "this
property doesn''t exist and never existed".

That is the sort of nonsense that makes it clear why your opi9nion is of
no value.

As a property with such value? (Gecko)
As non-existing property? (IE and Co)

Truthfully I would feel like to drop a coin, and then go to
Switzerland to hit some dumb heads :-)

The specification is clear on the subject, Mozilla/Gecko is (or was)
wrong and will be fixed so that it complies with the specification.

Richard.


这篇关于undefined vs. undefined(was:new Array()vs [])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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