1 回答

TA貢獻1875條經驗 獲得超3個贊
問題是toStringIIFE 的參數不在new Function代碼的范圍內。相反,它使用全局toString== 。window.toStringObject.prototype.toString
要解決此問題,您需要在 的代碼toString中聲明變量new Function以使返回的閉包起作用。作為一個簡單的常數:
(function() {
var pre_fn = new Function(`
const toString = Object.prototype.toString;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
return function(){
if(this instanceof TestClass) {
return '[object TestClass]';
}
return toString.apply(this, arguments);
}`);
Object.prototype.toString = pre_fn();
})();
或作為參數:
(function() {
var pre_fn = new Function('toString', `
// ^^^^^^^^^^^
return function(){
if(this instanceof TestClass) {
return '[object TestClass]';
}
return toString.apply(this, arguments);
}`);
Object.prototype.toString = pre_fn(Object.prototype.toString);
// ^^^^^^^^^^^^^^^^^^^^^^^^^
})();
添加回答
舉報