简体   繁体   English

javascript示例代码无法运行

[英]javascript example code cannot run

There is a piece of code of online tutorial 在线教程有一段代码

sayHi(1);

function sayHi(x); {
alert(x);           // 1
[].shift.call(arguments);  
alert(x);           // undefined, no x any more :/  
}

In tutorial it says: 在教程中它说:

Array methods which modify arguments also modify local parameters. 修改参数的数组方法也会修改局部参数。

Actually, the modern ECMA-262 5th specification splits arguments from local variables. 实际上,现代ECMA-262第5规范将参数与局部变量分开。 But as of now, browsers still behave like described above. 但是到目前为止,浏览器的行为仍与上述相同。 Try the examples to see. 试试看例子。

Generally, it is a good practice not to modify arguments. 通常,最好不要修改参数。

But I tried to run the above code, it output alert 1 twice instead of 1 and undefined. 但是我尝试运行上面的代码,它输出警报1两次,而不是1次,并且未定义。

http://javascript.info/tutorial/arguments http://javascript.info/tutorial/arguments

Could anyone please clarify that for me? 有人可以为我澄清一下吗? Thanks 谢谢

You simply forgot the semicolon. 您只是忘记了分号。 Change 更改

alert(x)

to

alert(x); 

Be careful with semicolon : yes there is semicolon insertion making most of them optional but the rules are so hard you should really write most of them. 对分号要小心:是的,有分号插入使得它们中的大多数是可选的,但是规则太难了,您应该真正编写其中的大多数。 Read more . 阅读更多

Regarding the modification of arguments , you simply shouldn't. 关于arguments的修改,您根本不应该这样做。 One of the reasons is that the behavior isn't the same when you're in strict mode , and it's not always easy to ensure you're not in strict mode because your code could have been concatenated with another one. 原因之一是在严格模式下的行为是不相同的 ,要确保您不在严格模式下并不总是那么容易,因为您的代码可能已与另一代码串联在一起。 The behavior mentioned in the bad and outdated tutorial you link to has been fixed since a long time. 很长时间以来,您链接到的糟糕且过时的教程中提到的行为已得到修复。

As destroy pointed out , the reason you got the syntax error was that (the horror that is) Automatic Semicolon Insertion couldn't correct the code correctly and it was important to add the semicolon explicitly. 正如destroy指出的那样 ,出现语法错误的原因是(令人恐惧的)自动分号插入无法正确地纠正代码,因此明确添加分号非常重要。

But to the meat of the question about the behavior of x vs. the arguments pseudo-array: 但是,关于x的行为与arguments伪数组的问题的实质是:

In loose mode (the default), there is a link between the named arguments and the arguments pseudo-array. 在宽松模式(默认)下,命名参数与arguments伪数组之间存在链接。 Here's a better demonstration of that link: 这是该链接的一个更好的演示:

 function foo(x) { snippet.log(x); // "one" snippet.log(arguments[0]); // also "one" x = "two"; snippet.log(x); // "two" snippet.log(arguments[0]); // also "two" arguments[0] = "three"; snippet.log(x); // "three" snippet.log(arguments[0]); // also "three" } foo("one"); 
 <!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

See how arguments[0] and x are basically synonyms of each other. 看看arguments[0]x在本质上是如何互为同义词。

The tutorial you refer to seems to believe that shifting the arguments pseudo-array (removing the first entry) will make x undefined because arguments[0] will be undefined at that point. 您所参考的教程似乎认为, arguments伪数组移位 (删除第一个条目)将使x undefined因为那时arguments[0] undefined While that would be a reasonable interpretation of the link between the named x and the arguments pseudo-array, it's not true on Chrome's V8, Firefox's SpiderMonkey, IE11's JScript, or even IE8's much older JScript. 虽然这可以合理地解释命名的xarguments伪数组之间的链接,但在Chrome的V8,Firefox的SpiderMonkey,IE11的JScript甚至是IE8的更老版本的JScript上却并非如此。

In strict mode, that link between the named arguments and the arguments pseudo-array doesn't exist: 在严格模式下,命名参数和arguments伪数组之间的链接不存在:

 function foo(x) { "use strict"; snippet.log(x); // "one" snippet.log(arguments[0]); // also "one" x = "two"; snippet.log(x); // "two" snippet.log(arguments[0]); // "one" again, it wasn't changed arguments[0] = "three"; snippet.log(x); // "two" again, it wasn't changed snippet.log(arguments[0]); // "three" } foo("one"); 
 <!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

In general, it's best to use strict mode, and to not rely on the link even in loose mode code. 通常,最好使用严格模式,即使在宽松模式代码中也不要依赖链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM