简体   繁体   中英

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 () { ...

With your code, someMethod2 would need to execute first for the function expression to be assigned. 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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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