4

I am currently coding in this way:

<script type="text/javascript">
var linkObj;

Is this a safe way to store data? My concern is what if a jQuery or other plug-in was to also use the variable linkObj. Also if I declare my variable like this then can it also be seen by other functions in scripts located in other js files that I include?

4 Answers 4

5
$(document).ready(function(){
   var linkObj;

});

as long as you use the var keyword, any variable defined in that scope won't be accessible by other plugins.

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

Comments

5

I you declare a variable this way it will be accessible to all scripts running on the page.

If you just want to use it locally, wrap it in a function:

(function() {var linkObj; ... })()

However, this way nothing outside of the function will be able to access it.

If you want to explicitly share certain variables between different scripts, you could also use an object as a namespace:

var myProject = {}
myProject.linkObj = ...

This will minimize how many global names you have to rely on.

4 Comments

If I do it this way then is it okay if more than one script declare var myProject = {} ? I am asking this because I am not sure where to declare the vmyProject namespace which will be shared around. Thanks.
No, more than one script could still declare myProject and mess up your bindings. However, if you give it a sufficiently unique name (e.g. the name of your project), you should not have any problems. If you're really paranoid, you can check whether a name is taken before assigning it.
I'm sorry I was not clear. What I meant was more than one of my own scripts. If two scripts both used the same namespace but I am not sure which of the scripts will run would that be okay?
In that case, just check whether the object already exists with something like: var myProject = myProject || {}. After this, you should be safe using it as a namespace. You can check whether properties on the object have been defined in much the same way.
3

Wrap it in a closure:

<script type="text/javascript">
(function() {
    var linkObj;
    // Rest of your code
})();
</script>

This way no script outside your own will have access to linkObj.

Comments

1

Is this a safe way to store data?

This is not storing data per se, it's only declaring a variable in a script block in what I assume is an HTML page. When you reload the page in the future, it will not hold previous values.

My concern is what if a jQuery or other plug-in was to also use the variable linkObj.

That's a valid concern, like others have pointed out. However, you would expect plugins not to rely on scope outside the plug-in. This shouldn't impact a lot as good plug-in design would likely prevent this from happening.

Also if I declare my variable like this then can it also be seen by other functions in scripts located in other js files that I include?

Yes. As long as their execution is triggered after your script block gets loaded. This normally follows the order in which your script declaration appears in the page. Or regardless of the order they appear on the page if they are executed, for example, after the jQuery DOM 'ready' event.

It's common to hear that is good to avoid 'global namespace pollution', which relates to this concern. To accomplish that you can use a function to contain code, and directly invoke that function in your script block.

(function () {
    var a = 1; // the scope is within the function

    alert('The variable a is equal to: ' + a);

}) (); // the parenthesis invoke the function immediately

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.