Javascript this is undefined. This should be global object

前端 未结 2 525
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 04:43

Why do I get the following error in the google dev console when loading the script from a web page?

But if I step through the code with the dev tool debugger the err

2条回答
  •  借酒劲吻你
    2021-01-17 05:09

    this is not bound to the global object in strict mode.

    The value of this is not converted to the global object when it is null or undefined. JavaScript

    function testFunc() {
        return this;
    }
    var testvar = testFunc();
    

    In non-strict mode, the value of testvar is the global object, but in strict mode the value is undefined.

    http://msdn.microsoft.com/en-us/library/br230269(v=vs.94).aspx

    That solves most of it. I still can't explain why it works while stepping through the code in the chrome debugger.

提交回复
热议问题