【问题标题】:Javascript | Pass an objects function as argument into another objectJavascript |将对象函数作为参数传递给另一个对象
【发布时间】:2017-12-20 03:44:23
【问题描述】:

我目前正在用 javascript 构建一个 GUI,我希望能够将一个对象函数作为参数传递给另一个对象,下面的代码演示了问题和预期的输出。

var Window = function(){
    this.close = function(){
        console.log(this)
    }
}


var Button = function(func){
    this.func = func;
    this.press = function(){
        this.func();
    }
}

var win = new Window();
var button = new Button(win.close);

button.press();
//Output: Button object
//Expected output: Window object

【问题讨论】:

标签: javascript object methods p5.js


【解决方案1】:
var Button = function(func){
    //this.func = func; this is not required.
    this.press = function(){
       func(); //just call func
    }
}

【讨论】:

    【解决方案2】:

    您应该将函数绑定到您希望this 引用的对象。查看此 MDN 参考以了解如何使用 Function#bind

    var Window = function(){
        
        this.close = function(){
            console.log(this)
        }
        this.close = this.close.bind(this)
    }
    
    
    var Button = function(func){
        this.func = func;
        this.press = function(){
            this.func();
        }
    }
    
    var win = new Window();
    var button = new Button(win.close);
    
    button.press();

    【讨论】:

      【解决方案3】:

      var Window = function(){
          var w = {};
          w.close = function(){
              console.log(w);
          }
          return w;
      }
      
      
      var Button = function(func){
          var b = {press: func};
          return b;
      }
      
      var win = new Window();
      var button = new Button(win.close);
      
      button.press();

      【讨论】:

        【解决方案4】:

        您可以保留这样的参考:

        var Window = function(){
            var context= this;
            this.close = function(){
                console.log(context)
                //output: Object { close: Window/this.close() }
            }
        }
        
        
        var Button = function(func){
            this.func = func;
            this.press = function(){
                this.func();
            }
        }
        
        var win = new Window();
        var button = new Button(win.close);
        
        button.press();

        【讨论】:

          【解决方案5】:

          您需要将外部“this”作为不同的变量传递给 press(),如下所示。我将“this”分配给“self”,并在 press() 中引用了它。这是我对您的代码所做的唯一更改。

          var Window = function(){
            this.close = function(){
              console.log(this)
            }
          }
          
          
          var Button = function(func){
             let self = this;      // this is the change I made
             this.func = func;
             this.press = function(){
               self.func();        // this is the change I made
            }
          }
          
          var win = new Window();
          var button = new Button(win.close);
          
          button.press();
          

          【讨论】:

            猜你喜欢
            • 2018-11-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-08-29
            • 1970-01-01
            • 2018-02-23
            • 2012-05-30
            相关资源
            最近更新 更多