1

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

Community
  • 1
  • 1
lejahmie
  • 17,938
  • 16
  • 54
  • 77

5 Answers5

3

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!');
};
Andy E
  • 338,112
  • 86
  • 474
  • 445
2

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();

Gazler
  • 83,029
  • 18
  • 279
  • 245
  • But then calling just self.someMethod2() wouldn't do much right? – lejahmie Feb 07 '12 at 09:24
  • 1
    Calling self.someMethod2() will return the object literal. You can't have something act as both a function and an object literal. – Gazler Feb 07 '12 at 09:26
0

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!');
};
Ates Goral
  • 137,716
  • 26
  • 137
  • 190
0

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

Mike
  • 1,042
  • 1
  • 8
  • 14
0
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;
    };
}
xdazz
  • 158,678
  • 38
  • 247
  • 274