我想擴展函數原型來返回函數的單例版本。在Function.prototype中訪問「this」不可能?
Function.prototype.once = function() {
var called = false, memo;
return function() {
console.log('AP', this);
if (!called) memo = this.apply(this, arguments);
called = true;
return memo;
}
}
控制檯記錄窗口對象。這是爲什麼!=當前功能?以及如何解決這個問題?
快樂codewars :) – Florent 2014-09-29 13:28:38