【问题标题】:JavaScript refer to a method inside a method?JavaScript 在方法中引用方法?
【发布时间】:2012-02-07 09:16:17
【问题描述】:

好的,刚刚解决了this refered to the wrong scope 的一个问题。现在我有另一个问题。

所以我想将 insidemethod 称为 method .但我不知道怎么做,检查这个来源:

function someObj() {
   var self = this;

   this.someMethod1 = function() {
      var elementBtn = document.getElementById('myBtn');

      elementBtn.onclick = function() { 
         self.someMethod2.methodMethod(); 
         //I want this.someMethod2.methodMethod() to be called
         //...but I get an big error instead. Is it even possible?
         //this.someMethod2() works fine.

      };
   };
   this.someMethod2 = function() {
      this.methodMethod = function() {
         alert('THIS IS THE ONE I WANTED!');
      };
      alert('NO, NOT THIS!');
   };
}

错误信息:

Uncaught TypeError: Object function () { ...

【问题讨论】:

    标签: javascript javascript-objects


    【解决方案1】:

    使用您的代码,someMethod2 需要首先执行才能分配函数表达式。即使这样,它也会被分配给父实例。

    请记住,所有函数都是 JavaScript 中的对象,这就是您想要的:

    this.someMethod2 = function() {
       alert('NO, NOT THIS!');
    };
    this.someMethod2.methodMethod = function() {
       alert('THIS IS THE ONE I WANTED!');
    };
    

    【讨论】:

      【解决方案2】:

      您正试图在函数上使用对象访问器。如果您希望它以这种方式工作,您需要从对“外部”函数的调用中返回一个对象字面量。

      this.someMethod2 = function() {
        return {
          methodMethod: function() {
            alert('THIS IS THE ONE I WANTED!');
          }
        }
      };
      

      然后您可以链接调用。 self.someMethod2().methodMethod();

      【讨论】:

      • 但是只调用 self.someMethod2() 不会做很多事情吧?
      • 调用 self.someMethod2() 将返回对象字面量。你不能让某个东西既作为函数又作为对象字面量。
      【解决方案3】:

      虽然这不是直接可能的,但您可以将“命令”传递给外部函数,告诉它执行内部函数。但是,你确定这是你真正需要的吗?也许您应该在这里使用对象而不是函数。但这里是“命令”方式:

      this.someMethod2 = function(cmd) {
          var methodMethod = function() {
              alert('THIS IS THE ONE I WANTED!');
          };
      
          if (cmd === "methodMethod") {
              methodMethod();
              return;
          }
      
          alert('NO, NOT THIS!');
      };
      

      【讨论】:

      • 你能举个例子吗?我对 JS 中的对象和函数有点困惑。 ^^
      【解决方案4】:
      function someObj() {
          var self = this;
      
          this.someMethod1 = function () {
              var elementBtn = document.getElementById('myBtn');
      
              elementBtn.onclick = function () {
                  self.someMethod2().methodMethod();
              };
          };
          this.someMethod2 = function () {
              this.methodMethod = function () {
                  alert('THIS IS THE ONE I WANTED!');
              };
              //return this for chain method.
              return this;
          };
      }
      

      【讨论】:

        【解决方案5】:

        尝试

        function someObj() {
         var self = this;
        
         this.someMethod1 = function() {
          var elementBtn = document.getElementById('myBtn');
          elementBtn.onclick = function() { 
              self.someMethod2().methodMethod(); 
          };
        
         this.someMethod2 = function() {
        
          this.methodMethod = function() {
             alert('THIS IS THE ONE I WANTED!');
          };
          alert('NO, NOT THIS!');
          return this;
         };
        }
        

        如果你使用 prototype 那么

        function someObj() {
         var self = this;
        
          this.someMethod1 = function() {
            var elementBtn = document.getElementById('myBtn');
            elementBtn.onclick = function() { 
              self.someMethod2.methodMethod();//['methodMethod'](); 
            };
           };
        
           this.someMethod2 = function() {
        
        
         };
         this.someMethod2.methodMethod = function() {
             alert('THIS IS THE ONE I WANTED!');
           };
         };
        

        但是方法methodMethod是静态的

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-30
          • 1970-01-01
          相关资源
          最近更新 更多