0

A piece of code from my javascript file

$(document).ready(function(){
    gsdk.init();
});

// Note: No var in the prefix of gsdk and it is also declared only here
gsdk = {
    init : some function...
}

When I add this javascript file in html page and run, it gives

Uncaught ReferenceError: gsdk is not defined(…)

8
  • Have you tried moving gsdk = {} in front of $(document).ready()? Commented Nov 21, 2016 at 7:05
  • sorry ); is there I missed it... Commented Nov 21, 2016 at 7:07
  • You are missing the Jquery Plugin. Commented Nov 21, 2016 at 7:07
  • moving gsdk = {} any different if both the function and variable are in the global execution context Commented Nov 21, 2016 at 7:08
  • This <script src="js/jquery-1.11.3.min.js"></script> is there in html file before adding my javascript file Commented Nov 21, 2016 at 7:09

2 Answers 2

4

That code is relying on The Horror of Implicit Globals¹, where assigning to an undeclared identifier creates a global variable.

To get the error you've described, the ready "event" would have to have already fired before your code is run, because jQuery's ready feature is chaotic: If the page is already ready, it calls its callback synchronously; if not, it calls it asynchronously.

If it were calling the callback asynchronously, you wouldn't be getting an error, because the code assigning to gsdk would have run before the code in the callback, creating the global and giving it its value.

In any case, the fix is:

  1. Declare gsdk, don't rely on the horror of implicit globals, and

  2. Move the initialization of it above your ready code, so that if your ready callback is called synchronously, gsdk is initialized and ready for use.

E.g.:

// 1. Declare the variable, don't rely on The Horror of Implicit Globals.
// 2. Make certain it has its value before setting up your `ready` callback, since
//    apparently you're doing this when the page is already ready.
var gsdk = {
    init : some function...
};

$(document).ready(function(){
    gsdk.init();
});

¹ (That's a post on my anemic little blog.)

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

1 Comment

As @Justinas suggested when I placed var prefix to gsdk and moved it before the document ready call its fixed.
1

You have syntax error .It should work

Though gsdk is a global variable here ..Not declaring with var should not a problem,but it would create a variable on the global object i.e., window.gsdk/gsdk is same here and even you are declaring gsdk it after invocation it does not throw error because variables are hoisted your code is similar to this below

var gsdk;
$(document).ready(function(){
    gsdk.init();
});

gsdk = {
    init : function(){
       alert("hi");
     }
}

$(document).ready(function(){
    gsdk.init();
});

gsdk = {
    init : function(){
       alert("hi");
     }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

3 Comments

Ya but when I check my window object a gsdk parameter is there but it is undefined
did you check it as window.gsdk
@akar: That's because (as I said in my answer), your ready callback is getting called synchronously. If you just make the change above, gsdk will be declared but will have the value undefined.

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.