简体   繁体   English

JavaScript'使用严格'; 内部功能

[英]JavaScript 'use strict'; inside functions

Tested some js code in Chrome Dev Console and I'm a bit confused. Chrome Dev Console中测试了一些js代码,我有点困惑。

I know that in strict mode functions that are not methods of an object when referred to this keyword should receive undefined instead of global object. 我知道在严格模式下 ,当引用关键字时,不是对象方法的函数应该接收undefined而不是global对象。

function test(){
    "use strict";
    return this===undefined;}
test(); 

Outputs false . 输出错误

"use strict";
function test(){
    return this===undefined;}
test(); 

Still false . 还是假的

(function test(){
    "use strict";
    return this===undefined;}());

Outputs true . 输出为

Just wanted to clarify. 只是想澄清一下。 ʕ •ᴥ•ʔ I'm new to js. ʕ•ᴥ•ʔ我是js的新手。

What you have noticed is simply a side-effect of the way the developer console works. 你注意到的只是开发者控制台工作方式的副作用。 When you enter code there, this is effectively what happens (see this answer for more details): 当您在那里输入代码时,实际上会发生这种情况(有关更多详细信息,请参阅此答案 ):

eval.call(null, "with (window) { \
                     function test() { \
                         'use strict'; \
                         console.log(this); \
                     } test(); \
                 }");

This is an indirect call to eval , which means it will always execute in the global execution context (in the browser, that's window ). 这是对eval间接调用,这意味着它将始终在全局执行上下文中执行(在浏览器中,即window )。

Effectively, the function is bound to the global object and therefore this holds a reference to the global object, as if you did this in a web page (rather than in the console): 实际上,该函数绑定到全局对象,因此this保存对全局对象的引用,就像您在网页中(而不是在控制台中)这样做:

function test(){
    "use strict";
    return this === undefined;
}

test(); // true
test.call(window); // false

Everything's fine. 一切安好。 If you run your code via some HTML page (not a dev console), results meet expectations (always this===undefined ). 如果您通过某个HTML页面(而不是开发控制台)运行代码,则结果符合预期(总是this===undefined )。

Additionally in latest Firefox (Firebug): 另外在最新的Firefox(Firebug)中:

function test(){
    "use strict";
    return this===undefined;}
test(); 
>> true

So this seems to be just another Chrome's bug (feature?). 所以这似乎只是另一个Chrome的错误(功能?)。 It feels like it has a slightly different approach to the code that is passed via dev console. 感觉它与通过开发控制台传递的代码略有不同。

Also note that order matters: 还要注意订单的重要性:

<script>
    console.log( 'Me First!' );

    "use strict";

    function test(){
        console.log( this );
    }
    test();

</script>

>>> "Me First!"
>>> Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}

But: 但:

<script>
    "use strict";

    console.log( 'Me later!' );

    function test(){
        console.log( this );
    }
    test();

</script>

>>> undefined
>>> "Me later!"

It is a bug in the Chromium Developer Console that causes this to still refer to the global object. Chromium Developer Console中的一个错误导致this仍然引用全局对象。 The same code works as specified with javascript: in the location bar, and in documents. 使用javascript:在位置栏和文档中指定的相同代码。

You can test that like so (2 console inputs): 您可以像这样测试(2个控制台输入):

var global = (function () { return this; }());

"use strict";
function test () { return this === global; }
test();

and (one or more console inputs) 和(一个或多个控制台输入)

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);

Tested in Chromium Version 25.0.1364.97 Debian 7.0 (183676). 在Chromium版本25.0.1364.97 Debian 7.0(183676)中测试。

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

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