1

I am studying a JavaScript file and saw in it that some of the methods are wrapped inside a jQuery function. Can Anyone help me how to invoke the following method? And may I know what is the advantage or why the method is wrapped in a function? Below is my sample JavaScript code.

JQuery/JavaScript

$(document).ready(function () {
    //How to invoke "testMethod" method?
    $(function () {
        function testMethod() {
            alert("this is a test method");
        }
    });
});
6
  • 4
    Why would you use $(document).ready inside of $(document).ready? $(function(){}); is the shorthand version of $(document).ready Commented Apr 8, 2013 at 5:55
  • How do you intend to invoke it? DOM event? or from another function? Commented Apr 8, 2013 at 5:57
  • @Travis J thanks for the info now it answers my question "WHY" Commented Apr 8, 2013 at 5:57
  • @Joseph the Dreamer I wish to call it from another function Commented Apr 8, 2013 at 6:01
  • @JobertEnamno and this function lives outside the document.ready? Commented Apr 8, 2013 at 6:02

4 Answers 4

3

As you've declared it, testMethod() is a local function and is only available inside the function scope in which it is declared. If you want it to be callable outside that scope, you will need to define it differently so that it is available at a broader scope.

One way of doing that is to make it a global function:

$(document).ready(function () {
    //How to invoke "testMethod" method?
    $(function () {
        window.testMethod = function() {
            alert("this is a test method");
        }
    });
});

testMethod();   // available globally now

It could also be attached to a global namespace or it could be defined at a higher scope where it would also solve your problem. Without specifics on your situation, we can't suggest which one would be best, but the main thing you need to do is to change how the function is declared so it is available in the scope in which you want to call it from.

P.S. Why do you have one document ready function nested inside another? That provides no extra functionality and adds unnecessary complexity. Also, there's really no reason to define testMethod() inside your document ready handlers if you want it available globally.

Sign up to request clarification or add additional context in comments.

8 Comments

Don't make them global. Assign them to a different scope, but don't make them global.
@IngoBürk - it depends upon what the OP is trying to do. If they provide more details, we can provide other options, but their code is obviously just an example for the purposes of the question so we have no idea what they are really doing or trying to solve. Global functions are useful in some circumstances as are other constructs which I've also mentioned in my answer.
(Not downvoter) That is one way to do it, but definitely not a desirable way. You could always attach it to another global variable...such as using the available functionality jquery provides with $.fn. At least that would leverage existing structures.
@jfriend00 useful in some cases or not, talking about global functions without the proper warning imho always is bad advice. Maybe you could cover it in your answer? I'd be happy to change my vote then.
@IngoBürk - I added some other info to my answer, but I'm not going to turn my answer into at treatise about global variables. The OP has no specifics on what is really required here or not. I've answered the question that the scope of the definition needs to be changed and offered several ways that can be done.
|
1

Before anything else:

$(document).ready(function(){...});
//is the same as
$(function(){...}}

As for your question, here's are potential ways to do it:

  • If that function is some utility function that everyone uses, then have it available to all in some namespace, like in this one called Utility:

    //Utility module
    (function(ns){
      //declaring someFunction in the Utility namespace
      //it's available outside the ready handler, but lives in a namespace
      ns.someFunction = function(){...}
    }(this.Utility = this.Utility || {}));
    
    $(function(){
      //here in the ready handler, we use it
      Utility.someFunction();
    });
    
  • If they all live in the ready handler, and want it to be used by all code in the handler, have it declared in the outermost in the handler so all nested scopes see it.

    $(function(){
      //declare it in the outermost in the ready handler
      function someFunction(){...}
    
      //so we can use it even in the deepest nesting
      function nestedSomeFunction(){
        someFunction();
      }
    
      someElement.on('click',function(){
        $.get('example.com',function(){
          someFunction();
        });
      });
    
      nestedSomeFunction();
      someFunction();
    
    });
    

Comments

0

Your call needs to be within the $(function.

It's all about scope and you need to break the testMethod out of the $(function.

Can you perhaps further explain your requirement so that we can maybe help a little better?

Comments

0

Into ready event:

$(document).ready(function () {
    //How to invoke "testMethod" method?
    var testMethod = function () {
        alert("this is a test method");
    }

    // V0.1
    testMethod();

    // V0.2
    $('#some_id').click(testMethod);
});

In other part:

myObj = {testMethod: null};
$(document).ready(function () {
    //How to invoke "testMethod" method?
    myObj.testMethod = function () {
        alert("this is a test method");
    }
});

// Something else
if( myObj.testMethod ) myObj.testMethod();

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.