你能告诉我为什么吗? [英] Could you tell me why ?

查看:56
本文介绍了你能告诉我为什么吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< html>< head>< / head>< body>< script>


(function(){

var watch =" Hola!" ;;

(函数f(){alert(watch)})();

alert(typeof f);

})();


< / script>< / body>< / html>


此收益率:


Safari:" Hola!",undefined。

FireFox:" function()watch {[native code]}" ;,未定义


你能否(请,请)告诉我原因:


1.- FF将手表变成本机功能。

2.- f仍未定义。


TIA,

- 乔治。

解决方案

5月7日上午10点19分,Jorge写道:


< html>< head> < / head>< body>< script>


(function(){

var watch =" Hola!";

(函数nf(){alert(watch)})();

alert(typeof f);


})();


< / script>< / body>< / html>


这产生:


Safari: Hola!,undefined。

FireFox:" function()watch {[native code]}"," undefined"


你能否(请,请)告诉我原因:


1.- FF将手表变成原生功能。

2.- f仍然存在未定义。



JavaScript(tm)对象有 - watch - 和 - unwatch - 方法(作为调试的b / b
)。当一个函数表达式提供了

可选标识符(在这种情况下你的f)时,指定的行为是

来创建相应的函数对象对象

在范围链顶部,由函数'

内部[[Scope]]属性引用,并为该对象指定一个名称

对应于Identifier和一个引用

函数对象的值。这样做的一个副作用是,当函数执行
时,它的作用域链上有一个普通的javascript对象,并且

所以函数中使用的所有标识符都与

普通javascript对象的命名属性将解析为

属性,如果没有被正式掩盖的那个特定对象,b = b参数,局部变量或内部函数声明。而且,普通的javascript对象有一个 - watch - 属性(例如在

JavaScript(tm)中),那么标识符 - 监视 - 将解析为

对-watch的引用 - 该对象的方法。


f仍未定义因为具有可选

标识符的FunctionExpression不会导致为其执行上下文创建

激活/变量对象的命名属性

评估(它不是FunctionDeclaration,它会)。那是

,直到你在IE中尝试这个并看到使用可选的

标识符与FunctionExpressions这样一个坏主意的错误,你将是

最好不要尝试。


5月7日,12:46 * pm,Henry< rcornf ... @ raindrop.co.ukwrote:


JavaScript(tm)对象有 - watch - 和 - unwatch - 方法(作为调试的b / b
)。当一个函数表达式提供了

可选标识符(在这种情况下你的f)时,指定的行为是

来创建相应的函数对象对象

在范围链顶部,由函数'

内部[[Scope]]属性引用,并为该对象指定一个名称

对应于Identifier和一个引用

函数对象的值。这样做的一个副作用是,当函数执行
时,它的作用域链上有一个普通的javascript对象,并且

所以函数中使用的所有标识符都与

普通javascript对象的命名属性将解析为

属性,如果没有被正式掩盖的那个特定对象,b = b参数,局部变量或内部函数声明。而且,普通的javascript对象有一个 - watch - 属性(例如在

JavaScript(tm)中),那么标识符 - 监视 - 将解析为

引用该对象的-watch - 方法。



啊......所以事先有一个.watch()方法......!

但不是在Safari ......等等对吗?

仅限FireFox?

并且,该方法是标准的一部分吗?

为什么要编写*** JavaScript( tm)***?


f仍未定义因为具有可选

标识符的FunctionExpression不会导致为其执行上下文创建

激活/变量对象的命名属性

评估(它不是FunctionDeclaration,它会)。那是

,直到你在IE中尝试这个并看到使用可选的

标识符与FunctionExpressions这样一个坏主意的错误,你将是

最好不要尝试它。



嗯,我期待(函数funcName(){})();在这种情况下产生一个定义的

funcName。

为什么这是一个坏主意?

你总是可以使用一个未命名的函数,我的意思是,如果你

并不打算定义funcName ...?


(function(){

var watch = aVar =" Hola!" ;;

(函数f(){

alert(aVar);

alert(观看);

提醒(typeof f);

})();

alert(typeof f);

})();


IE8b:Hola!,Hola!,function,function等。 < - 你是对的,

IE做到了!

Safari:Hola!,Hola!,function, " undefined"

FireFox:Hola!,native function(),function,undefined


感谢分享,

--Jorge。


5月7日下午12:28,Jorge写道:


5月7日下午12:46,Henry写道:


> JavaScript(tm)对象有 - 监视 - 和 - unwatch - 方法...



< snip>


啊......所以事先有一个.watch()方法......!



是的。


但不是在Safari ......等等吗?



尚未,但Safari可以随时添加一个。 Opera的ECMAScript

实现了 - watch - 和unwatch - 其对象上的方法

与JavaScript(tm)的兼容性,Safari确实为
类似的兼容性。


仅限FireFox?



不仅(假设FireFox是指基于Mozilla / Gecko的所有基于Web浏览器的所有

(并且那里)现在只有20个奇数来自



那么,该方法是标准的一部分吗?



编号但该标准允许实现提供扩展和

这些方法是扩展。


你为什么写*** JavaScript(tm)***?



JavaScript,大写字母J和大写字母S,是商标

现在单个ECMAScript实现的名称(

源于Netscape的名称,现在是Mozilla

基金会的责任,并出现在Firefox中有/或
浏或''Javascript''(带有非

实现(和许多其他实施一样)和商标名称来引用

明确地具体实现(特别是

"(tm)" (他们有这样的名字(我知道这些名字是什么)。


> f仍未定义因为带有
可选标识符的FunctionExpression不会导致为评估它的
执行上下文创建激活/变量对象的命名属性(它不是
FunctionDeclaration,哪个会)。这是直到你在IE中尝试这个,并看到使用FunctionExpressions使用可选的
标识符的错误,这样一个坏主意,
你最好不要尝试它。



嗯,我在期待(函数funcName(){})();在该上下文中生成定义的

funcName。



但规范说不应该发生。函数表达式

和FunctionDeclarations的处理方式不同,无论

在某些情况下它们的相似程度如何。


为什么这是一个坏主意?



哪个?认为它应该创建变量

对象的命名属性或使用带有FunctionExpressions的可选标识符?

前者是一个坏主意,因为它与规范不一致,并且该规范的大多数实现的实际情况都是b b $ b。后者

是一个坏主意,因为IE不符合这个

区域的规格,它做了什么就足够奇怪了

有问题。


你总是可以使用一个未命名的函数,

我的意思是,如果你不打算得到funcName定义了......?



使用带有FunctionExpressions的可选标识符是

允许函数体内的代码通过

名称(而不是使用 - arguments.callee - )。


(function(){

var watch = aVar =" Hola!" ;;

(函数f(){

alert(aVar);

alert(watch);

alert(typeof f);

})();

alert(typeof f);



^^^^^^^^^^^^^^

尝试将此行移至评估函数

表达式以上,看看如何严重的IE在这里表现。


>

})();


IE8b :Hola!,Hola!,function,function, < - 你说得对,

IE做到了!



< snip>

是的,如果你这样你会发现功能对象

结果形式对函数表达式的评估是不一样的

函数对象,可以使用

包含上下文中的Identifier来引用。


<html><head></head><body><script>

(function () {
var watch = "Hola !";
(function f () { alert(watch) })();
alert(typeof f);
})();

</script></body></html>

This yields:

Safari : "Hola !", "undefined".
FireFox : "function () watch { [native code] }", "undefined"

Could you (kindly, please) tell me why :

1.- FF turns watch into a native function.
2.- f remains undefined.

TIA,
--Jorge.

解决方案

On May 7, 10:19 am, Jorge wrote:

<html><head></head><body><script>

(function () {
var watch = "Hola !";
(function f () { alert(watch) })();
alert(typeof f);

})();

</script></body></html>

This yields:

Safari : "Hola !", "undefined".
FireFox : "function () watch { [native code] }", "undefined"

Could you (kindly, please) tell me why :

1.- FF turns watch into a native function.
2.- f remains undefined.

JavaScript(tm) objects have - watch - and - unwatch - methods (as an
aid to debugging). When a Function Expression is provided with the
optional Identifier (your ''f'' in this case) the specified behaviour is
to create the corresponding function object with an additional object
at the top of the scope chain that is referred to by the function''s
internal [[Scope]] property and give that object a name which
corresponds with the Identifier and a value that refers to the
function object. A side effect of this is that when the function is
executed it has an ordinary javascript object on its scope chain and
so all Identifiers used within the function that correspond with the
named properties of ordinary javascript objects will resolve as
properties of that particular object if not masked by formal
parameter, local variable or inner function declarations. And where
the ordinary javascript object have a - watch - property (such as in
JavaScript(tm)) then the Identifier - watch - will resolve as a
reference to the -watch - method of that object.

f "remains undefined" because the FunctionExpression with optional
Identifier does not result in the creation of a named property of the
Activation/Variable object for the execution context in which it is
evaluated (it is not a FunctionDeclaration, which would). That is
until you try this in IE and see the bug that makes using the optional
Identifiers with FunctionExpressions such a bad idea that you would be
best off never attempting it.


On May 7, 12:46*pm, Henry <rcornf...@raindrop.co.ukwrote:

JavaScript(tm) objects have - watch - and - unwatch - methods (as an
aid to debugging). When a Function Expression is provided with the
optional Identifier (your ''f'' in this case) the specified behaviour is
to create the corresponding function object with an additional object
at the top of the scope chain that is referred to by the function''s
internal [[Scope]] property and give that object a name which
corresponds with the Identifier and a value that refers to the
function object. A side effect of this is that when the function is
executed it has an ordinary javascript object on its scope chain and
so all Identifiers used within the function that correspond with the
named properties of ordinary javascript objects will resolve as
properties of that particular object if not masked by formal
parameter, local variable or inner function declarations. And where
the ordinary javascript object have a - watch - property (such as in
JavaScript(tm)) then the Identifier - watch - will resolve as a
reference to the -watch - method of that object.

Ahh.., so there was beforehand a .watch() method... !
But not in Safari... etc right ?
Only in FireFox ?
And, is that method a part of the standard ?
Why do you write ***JavaScript(tm)*** ?

f "remains undefined" because the FunctionExpression with optional
Identifier does not result in the creation of a named property of the
Activation/Variable object for the execution context in which it is
evaluated (it is not a FunctionDeclaration, which would). That is
until you try this in IE and see the bug that makes using the optional
Identifiers with FunctionExpressions such a bad idea that you would be
best off never attempting it.

Hmm, I was expecting (function funcName () {})(); to yield a defined
funcName in that context.
Why is that a bad idea ?
You could always have used an unnamed function instead, I mean, if you
didn''t intend to get funcName defined... ?

(function () {
var watch = aVar = "Hola !";
(function f () {
alert(aVar);
alert(watch);
alert(typeof f);
})();
alert(typeof f);
})();

IE8b : "Hola !", "Hola !", "function", "function" <- You''re right,
IE did it !
Safari : "Hola !", "Hola !", "function", "undefined"
FireFox : "Hola !", "native function ()", "function", "undefined"

Thanks for sharing,
--Jorge.


On May 7, 12:28 pm, Jorge wrote:

On May 7, 12:46 pm, Henry wrote:

>JavaScript(tm) objects have - watch - and - unwatch - methods ...

<snip>

Ahh.., so there was beforehand a .watch() method... !

Yes.

But not in Safari... etc right ?

Not yet, but Safari could add one at any moment. Opera''s ECMAScript
implementation has - watch - and unwatch - methods on its object for
compatibility with JavaScript(tm), and Safari does do some things for
similar compatibility.

Only in FireFox ?

Not only (and that is assuming that "FireFox" is taken to mean all
Mozilla/Gecko based web browsers (and there are 20 odd of them by
now).

And, is that method a part of the standard ?

No. But the standard allows implementations to provide extensions and
those methods are an extension.

Why do you write ***JavaScript(tm)*** ?

JavaScript, with an uppercase J and an upper case S, is the trademark
name of what is now a single ECMAScript implementation (the one that
originated at Netscape, is now the responsibility of the Mozilla
foundation and appears in Firefox/Mozilla/Gecko browsers). There are
numerous other ECMAScript implementations, such as JScript(tm) in IE
browsers. I tend to use ''javascript'' or ''Javascript'' (with the non-
trademark capitalisation) to refer to geniality of ECMAScript
implementations (as do many others) and the trademark names to refer
to the specific implementations unambiguously (especially with the
"(tm)" extension) (were they have such names (and I know what those
names are)).

>f "remains undefined" because the FunctionExpression with
optional Identifier does not result in the creation of a
named property of the Activation/Variable object for the
execution context in which it is evaluated (it is not a
FunctionDeclaration, which would). That is until you try
this in IE and see the bug that makes using the optional
Identifiers with FunctionExpressions such a bad idea that
you would be best off never attempting it.


Hmm, I was expecting (function funcName () {})(); to yield a defined
funcName in that context.

But the specification says that should not happen. FunctionExpressions
and FunctionDeclarations are not handled the same way regardless of
how similar they may appear in some cases.

Why is that a bad idea ?

Which? Thinking it should create a named property of the variable
object or using the optional Identifiers with FunctionExpressions? The
former is a bad idea because it is at odds with the specification and
the reality of most implementations of that specification. The latter
is a bad idea because IE does not conform to the specification in this
area and what it does do is sufficiently strange as to be
problematic.

You could always have used an unnamed function instead,
I mean, if you didn''t intend to get funcName defined... ?

The use of the optional Identifier with FunctionExpressions is to
allow code inside the function body to refer to the function object by
name (rather than using - arguments.callee -).

(function () {
var watch = aVar = "Hola !";
(function f () {
alert(aVar);
alert(watch);
alert(typeof f);
})();
alert(typeof f);

^^^^^^^^^^^^^^
Try moving this line to above the evaluation of the function
expression to see how badly IE is behaving here.

>
})();

IE8b : "Hola !", "Hola !", "function", "function" <- You''re right,
IE did it !

<snip>
Yes, and if you it you would discover that the function object that
results form the evaluation of the function expression is not the same
function object as can be referred to using the Identifier in the
containing context.


这篇关于你能告诉我为什么吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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