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

5 Answers 5

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!');
};
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

But then calling just self.someMethod2() wouldn't do much right?
Calling self.someMethod2() will return the object literal. You can't have something act as both a function and an object literal.
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!');
};

1 Comment

Could you give me an example? I am a bit confused with objects and functions in JS. ^^
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;
    };
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.