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
的开销)