如何在严格模式下定义[this]?

Lyn*_*ynn 3 javascript this strict-mode

当我在Chrome中运行此代码时,我得到一个未捕获的TypeError:无法读取未定义的属性'concat'

function _log()
{
    'use strict'

    [this].concat(Array.from(arguments)).forEach(
        function (obj) { console.log(obj) }
    )
}

_log('foo', 'bar');
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会这样.怎么可以[this]不定义?即使this未定义,[this]仍然应该是一个数组,不应该吗?

一个有趣的细节是,当我use strict从函数中删除行时,错误消失,代码按预期运行,将函数上下文和参数记录在一个新行上.

this在严格模式下使用关键字可能有些特别之处我不知道吗?

谢谢.

Den*_*ret 5

这是一个有趣的错误:

你刚忘了分号'use strict',这完全改变了代码的解析方式:

'use strict'[this].concat...
Run Code Online (Sandbox Code Playgroud)

你正在拿着"[Object window]"' use strict'链中的名字.当然是undefined,所以它没有任何叫做的属性"concat".