jus*_*tem 6 javascript strict ecmascript-5
在Chrome Dev Console中测试了一些js代码,我有点困惑.
我知道在严格模式下,当引用此关键字时,不是对象方法的函数应该接收undefined而不是global对象.
function test(){
"use strict";
return this===undefined;}
test();
Run Code Online (Sandbox Code Playgroud)
输出错误.
"use strict";
function test(){
return this===undefined;}
test();
Run Code Online (Sandbox Code Playgroud)
还是假的.
(function test(){
"use strict";
return this===undefined;}());
Run Code Online (Sandbox Code Playgroud)
输出为真.
只是想澄清一下.ʕ•ᴥ•ʔ我是js的新手.
这是 Chromium 开发者控制台中的一个错误,导致this仍然引用全局对象。相同的代码按照javascript:地址栏和文档中指定的方式工作。
您可以像这样测试(2 个控制台输入):
var global = (function () { return this; }());
"use strict";
function test () { return this === global; }
test();
Run Code Online (Sandbox Code Playgroud)
和(一个或多个控制台输入)
var script = document.createElement("script");
script.type = "text/javascript";
script.appendChild(document.createTextNode(
'function test () { "use strict"; return this === undefined; }; console.log(test());'
));
document.body.appendChild(script);
Run Code Online (Sandbox Code Playgroud)
在 Chromium 版本 25.0.1364.97 Debian 7.0 (183676) 中测试。
| 归档时间: |
|
| 查看次数: |
1413 次 |
| 最近记录: |