【发布时间】:2016-08-26 12:37:29
【问题描述】:
有一个非常简单的算法,它在两种情况下的工作方式出人意料地不同,这取决于"use strict" 的存在。
案例 1:
如果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
这种差异的根源是什么?这种转换的原因是什么?
这么简单的动作怎么会在严格模式的各种状态下如此不同?
【问题讨论】:
-
它在规范中“作为
this传递给严格模式下的函数的值不会被强制成为一个对象(又名“盒装”)”
标签: javascript google-chrome use-strict strict-mode