ecmascript 5 - JavaScript 'use strict'; inside functions - Stack Overflow

Tested some js code in Chrome Dev Console and I'm a bit confused.I know that in strict mode functi

Tested some js code in Chrome Dev Console and I'm a bit confused.

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.

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.

Tested some js code in Chrome Dev Console and I'm a bit confused.

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.

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.

Share asked Feb 28, 2013 at 12:18 user1343639user1343639 2
  • 2 please read a similar question stackoverflow./questions/1335851/… – Nick Andriopoulos Commented Feb 28, 2013 at 12:20
  • 4 @hexblot this doesn't answer the question though... – Christoph Commented Feb 28, 2013 at 12:37
Add a ment  | 

3 Answers 3

Reset to default 3

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

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

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

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

It is a bug in the Chromium Developer Console that causes this to still refer to the global object. The same code works as specified with javascript: in the location bar, and in documents.

You can test that like so (2 console inputs):

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

Everything's fine. If you run your code via some HTML page (not a dev console), results meet expectations (always this===undefined).

Additionally in latest Firefox (Firebug):

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

So this seems to be just another Chrome's bug (feature?). 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!"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745068256a4609386.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信