为什么 Firefox 正在注入 "use strict";在 class 方法中?
Why Firefox is injecting "use strict"; in class methods?
我有以下代码:
class A {
contructor(){
}
toString(){
return [this.b.toString(),this.toString.toString()].join('\n');
}
b(){
}
}
console.log(new A().toString());
function a (){}
a.prototype.b = function(){
}
a.prototype.toString = function toString(){
return [a.prototype.b.toString(), a.prototype.toString.toString()].join('\n');
}
console.log(new a().toString());
Firefox 中的输出是:
function b(){
"use strict";
}
function toString(){
"use strict";
return [this.b.toString(),this.toString.toString()].join('\n');
}
function (){
}
function toString(){
return [a.prototype.b.toString(), a.prototype.toString.toString()].join('\n');
}
从MDN可以看出"use strict"的用途:
Strict mode makes several changes to normal JavaScript semantics.
First, strict mode eliminates some JavaScript silent errors by
changing them to throw errors. Second, strict mode fixes mistakes that
make it difficult for JavaScript engines to perform optimizations:
strict mode code can sometimes be made to run faster than identical
code that's not strict mode. Third, strict mode prohibits some syntax
likely to be defined in future versions of ECMAScript.
但是好像没有人是这个原因。 Firefox 是否有理由将其注入功能?它是以编程方式执行的操作吗?
Strict mode
The bodies of class declarations and class expressions are executed in strict mode.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Strict_mode
非常不言自明。不多说了。
我有以下代码:
class A {
contructor(){
}
toString(){
return [this.b.toString(),this.toString.toString()].join('\n');
}
b(){
}
}
console.log(new A().toString());
function a (){}
a.prototype.b = function(){
}
a.prototype.toString = function toString(){
return [a.prototype.b.toString(), a.prototype.toString.toString()].join('\n');
}
console.log(new a().toString());
Firefox 中的输出是:
function b(){
"use strict";
}
function toString(){
"use strict";
return [this.b.toString(),this.toString.toString()].join('\n');
}
function (){
}
function toString(){
return [a.prototype.b.toString(), a.prototype.toString.toString()].join('\n');
}
从MDN可以看出"use strict"的用途:
Strict mode makes several changes to normal JavaScript semantics. First, strict mode eliminates some JavaScript silent errors by changing them to throw errors. Second, strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode. Third, strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.
但是好像没有人是这个原因。 Firefox 是否有理由将其注入功能?它是以编程方式执行的操作吗?
Strict mode
The bodies of class declarations and class expressions are executed in strict mode.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Strict_mode
非常不言自明。不多说了。