有一个非常简单的算法,它在两种情况下的工作方式会出人意料地不同,具体取决于"use strict"
.
案例一:
如果func()
声明在严格模式内,则控制台记录原语
"use strict";
// strict mode is on
Object.prototype.func = function() { return this; } // do nothing with the object
console.log( (4).func() ); // 4; primitive
案例2:
如果func()
声明超出严格模式,则控制台记录相同值的对象
// strict mode is off
Object.prototype.func = function() { return this; } // do nothing with the object
"use strict";
console.log( (4).func() ); // Number {[[PrimitiveValue]]: 4}; object
这种差异的根源是什么?这种转换的原因是什么?
这么简单的动作怎么会在严格模式的各种状态下如此不同?