0

How to call a function via string within a class?

$.fn.myApp = function (options){
  function myFunc(){
    //do something
  }
  window["myFunc"](); //not working
  $.fn.myApp["myFunc"](); //not working
}
7
  • Why do you need to call it via a string? Commented Sep 20, 2013 at 14:19
  • You may have simply omitted this in the question, but you do need () to execute the function, ex. window['alert']('hi');. Commented Sep 20, 2013 at 14:21
  • What exactly are you trying to accomplish? If you describe it a bit. Commented Sep 20, 2013 at 14:24
  • I think this question isn't so much about the bracket notation, but the context of functions nested in other functions. This SO question may be of help. Commented Sep 20, 2013 at 14:26
  • @Jason P: No, this syntax is not working in my case: TypeError: window.myFunc is not a function Commented Sep 20, 2013 at 15:15

2 Answers 2

1

I found a solution, maybe that helps anybody else as well!

$.fn.myApp = function (options){
  $.fn.myApp.myFunc = function(){
    //do something
  }
  $.fn.myApp["myFunc"](); //working
}
Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at this code: you need to expose the method in order to call it form outside that function: http://jsbin.com/aXuloFE/1/

1 Comment

I need to call the function within the class.

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.