简体   繁体   English

JavaScript是指方法内部的方法吗?

[英]JavaScript refer to a method inside a method?

Ok, just solved one problem where this refered to the wrong scope . 好的,只需解决一个问题,即它指向错误的范围 Now I have another problem. 现在我有另一个问题。

So I want to call a method that is inside a method . 所以我想调用一个方法 内部方法 But I do not know how, check this source: 但我不知道如何,请检查此来源:

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!');
   };
}

Error msg: 错误消息:

Uncaught TypeError: Object function () { ... 未捕获的TypeError:对象函数(){...

With your code, someMethod2 would need to execute first for the function expression to be assigned. 对于您的代码,必须首先执行someMethod2才能分配函数表达式。 Even then, it would be assigned to the parent instance. 即使那样,它也将被分配给父实例。

Bearing in mind that all functions are objects in JavaScript, this is what you want instead: 请记住,所有函数都是JavaScript中的对象,这是您想要的:

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

You are trying to use an object accessor on a function. 您正在尝试在函数上使用对象访问器。 If you want it to work in this way, you need to return an object literal from your call to the "outer" function. 如果希望它以这种方式工作,则需要从调用“外部”函数返回对象文字。

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

You can then chain the call. 然后,您可以链接呼叫。 self.someMethod2().methodMethod();

While this is not directly possible, you can pass a "command" to the outer function to tell it to execute the inner function. 虽然这不可能直接实现,但是您可以将“命令”传递给外部函数,以告诉它执行内部函数。 But, are you sure this is what you really need? 但是,您确定这是您真正需要的吗? Perhaps you should use objects instead of functions here. 也许您应该在这里使用对象而不是函数。 But here's the "command" way: 但是这是“命令”方式:

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

    if (cmd === "methodMethod") {
        methodMethod();
        return;
    }

    alert('NO, NOT THIS!');
};

trying

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;
 };
}

Also if you use prototype then 另外,如果您使用原型,

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!');
   };
 };

But the method methodMethod is static 但是方法methodMethod是静态的

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;
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM