tags:

views:

1509

answers:

9

I think I know the answer but... is there any way to prevent a global variable from being modified by later-executing <script>? I know global variables are bad in the first place, but when necessary, is there a way to make it "final" or "immutable"? Hacks / creative solutions are welcome. Thanks

A: 

Not that I know of. The best I can think of is storing the value on an object that won't likely be modified.

e.g.

navigator.pleaseDontChangeThis = 25;

It can still be changed, but most normal JS will have no need to set/change any values on the navigator object. I personally use this to store a unique generated ID. ;-)

scunliffe
+1  A: 

the const keyword?

Chandan .
why downvote? could you please give reason so that we know. I am not sure of my answer because I've never used it myself. So only I've put ques. mark in my answer. Moreover, when I tried in IE it failed. So, it'd be good if you clearify the reason for your downvote.
Chandan .
Not really sure why this got down voted, but this should work. At least, any browser that supports JS 1.5 or greater.
Steve Willard
yeah, thats what was my initial idea for posting. but then again I wasn't totally sure because I hadn't used it myself.
Chandan .
Does not work in latest Opera
presario
@Presario. Its a standard in JS 1.5. Just because it doesn't work in Opera doesn't prove anything. It didn't work for me in IE6 too. And in my answer I am not claiming that its gonna work. I just quoted a hyperlink which I thought may be of some help. Do you see a '?' in my answer?
Chandan .
JavaScript is Mozilla's version of ECMAScript; if you want your scipt to work across different browsers, stick to what's in ECMA-262, 3rd edition
Christoph
@Liverpool 4 - 1 Mancs. The question is not about standards, your suggestion/solution (whatever) does not work and therefore is of no use to the questioner. That's why it deserves a downvote.
presario
+1  A: 

Conventions and good Documentation.

You can prefix your "immutable" variable with two (or more) underscores to indicate that is something not meant to be used by others and to avoid other people's variables clashing with yours.

Maybe creating a 'namespace' like __GLOBALNAMESPACE (Ugly name, I know) and then adding your variables into it (eg __GLOBALNAMESPACE.my_var) and creating a method like this one to retrieve them:

getVariable(string name){ return __GLOBALNAMESPACE[name] }

Just my 2 cents. Regards.

Pablo Fernandez
+5  A: 

You can use closure technique, MYGLOBALS is an object that has a function called getValue against the "globals" associative array that is out of scope for everything except MYGLOBALS instance.

var MYGLOBALS = function() {
    var globals = {
     foo : "bar",
     batz : "blah"  
    }
    return { getValue : function(s) {
      return globals[s];
     }
    }
}();
print(MYGLOBALS.getValue("foo"));  // returns "bar"
print(MYGLOBALS.getValue("notthere")); // returns undefined
MYGLOBALS.globals["batz"] = 'hardeehar'; // this will throw an exception as it should
Dan
Nice try, but they can still replace the MYGLOBALS in one goal, anyway.
Dennis Cheung
Or replace the `getValue` property in MYGLOBALS with another function that returns different values.
Wyzard
Should those `print()` s be `alert()` s?
alex
+1  A: 
serioys sam
A: 

Choose a variable name which is unlikely to be overwritten by accident and trust the programmer to not do stupid things. JavaScript is not Java, so don't pretend it was.

Also, if what you really want to do is namespacing, use a self-executing function literal:

var myLibName = (function() {
    var aPrivateVar;

    function aPrivateFunction() {}

    function accessorForPrivateVar() {
        return aPrivateVar;
    }

    // public interface:
    return {
        getPrivateVar : accessorForPrivateVar
    }
})();
Christoph
A: 

try this:

const whatEver = 'Hello World!!!';

function foo(value){
 whatEver = value;
}

then you would call it like so...

<div onclick="foo('New Value');">Change Me First</div>
<div onclick="alert(whatEver);">Then click me After: Should Be alert "Hello World!!!"</div>
Val
A: 

yes the const is short for constant or final in some languages. google "javascript variable const" or constant to double i have even tested it myself so

const yourVar = 'your value';

thats what you are looking for.

Val
A: 

Really 'const' is working

Thanks buddy, great!!

Bhavesh