Jquery - scope inside $(document).ready()?

Javascript uses functional scopes, so local variables inside a function are not visible to the outside. This is why your code can't access code from other scopes.

Up vote 1 down vote favorite 1 share g+ share fb share tw.

So to stay organized, I have several javascript files, even though they all (in the end) are minified together to form one final javascript file. Each file's contents are wrapped in: $(document. Ready(function(){ //some javascript here }); It seems like if I have things in separate files (in between that code) they don't have access to each other.

Is this a scope issue? What can I do? For example, in one file I had a bunch of code to create tables from data received through ajax.

However, half of the file was just templates for how to display the data depending on it's types and such. I would like to have the templates in their own file. I understand this is just a 'preference' issue and that I could just have it all in one file.

But I'm hoping to learn from this and perhaps even be able to have it 'my' way. Jquery scope link|improve this question asked Dec 6 '10 at 4:05Matthew1,8461832 93% accept rate.

Javascript uses functional scopes, so local variables inside a function are not visible to the outside. This is why your code can't access code from other scopes. The ideal solution to this is create a Namespace.

Var NS = {}; (function(self){ function privateFunction() { ... } self. PublicFunction = function(){ ... } })(NS); $(document). Ready(function(){ NS.publicFunction(); }); This is also a useful pattern because it allows you to make a distinction between private & public elements.

I've written a blog post that goes into a little more detail about how I deal with complex JS in an enterprise app.

It is a scope issue. For example: function a() { var myHiddenStr = 'abc'; } alert(typeof(myHiddenStr)); You cannot access myHiddenStr outside of function a. In similar fashion, the anonymous function you use for document ready hides everything within it.

Having a global scope where you put things from different js files is not a good idea. It's probably better to have one document. Ready handler and call respective functions from within it.

You can then get the results out of the functions and pass them to other functions that need to use these.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions