Javascript 访问;这";在函数中。原型不可能吗?
我试图扩展函数原型以返回函数的单例版本Javascript 访问;这";在函数中。原型不可能吗?,javascript,Javascript,我试图扩展函数原型以返回函数的单例版本 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;
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;
}
}
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;
}).bind(this);
}
控制台记录窗口对象。这是为什么!=当前功能?那么如何解决这个问题呢?当然,这是可能的,但是您的内部函数会创建一个新的上下文,因此它内部的
这个与外部的这个不同
只需创建原始函数的外部引用:
Function.prototype.once = function() {
var f = this; // use 'f' in the inner function
...
}
return function() {
console.log('AP', this);
if (!called) memo = this.apply(this, arguments);
called = true;
return memo;
}.bind(this);
注意:根据您的意图,您可能对参数也有相同的问题当然,这是可能的,但是您的内部函数会创建一个新的上下文,因此它内部的this
与外部的this
不同
只需创建原始函数的外部引用:
Function.prototype.once = function() {
var f = this; // use 'f' in the inner function
...
}
return function() {
console.log('AP', this);
if (!called) memo = this.apply(this, arguments);
called = true;
return memo;
}.bind(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;
}
}
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;
}).bind(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;
}
}
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;
}).bind(this);
}
您不能“关闭”此
,因此您需要使用旧的var self=this
技巧(即,在可以关闭的变量中获取对此
的引用),或者简单地绑定您的函数:
Function.prototype.once = function() {
var f = this; // use 'f' in the inner function
...
}
return function() {
console.log('AP', this);
if (!called) memo = this.apply(this, arguments);
called = true;
return memo;
}.bind(this);
您不能“关闭”此
,因此您需要使用旧的var self=this
技巧(即,在可以关闭的变量中获取对此
的引用),或者简单地绑定您的函数:
Function.prototype.once = function() {
var f = this; // use 'f' in the inner function
...
}
return function() {
console.log('AP', this);
if (!called) memo = this.apply(this, arguments);
called = true;
return memo;
}.bind(this);
bind
是正确的方法。我会接受你的回答。很抱歉,@Florent,但是meagar比您快1秒:)IMHO,.bind
是一种过度杀伤力,只有在您不控制被调用函数的作用域(这样您才能关闭外部作用域变量)的情况下才应该使用。编写普通嵌套函数时,在别名上使用简单闭包更有效。bind
是正确的方法。我会接受你的回答。很抱歉,@Florent,但是meagar比您快1秒:)IMHO,.bind
是一种过度杀伤力,只有在您不控制被调用函数的作用域(这样您才能关闭外部作用域变量)的情况下才应该使用。编写普通嵌套函数时,在别名上使用简单闭包更有效。你说得对。为什么我没有想到这一点。但是我更喜欢bind
的方法:)@BarthZalewski,你认为.bind
是如何工作的?;-)另外,.bind
的开销是创建一个额外的函数
,而不仅仅是关闭对此
的额外引用(有关更多信息,请查看polyfill,查看.bind
的开销)。您是对的。为什么我没有想到这一点。但是我更喜欢bind
的方法:)@BarthZalewski,你认为.bind
是如何工作的?;-)另外,.bind
的开销是创建一个额外的函数
,而不仅仅是关闭对此
的额外引用(有关更多信息,请查看polyfill,查看.bind
的开销)