Hello all,
I have a simple question. There is this keyword const in javascript, has anyone ever used it? I had never had to use it so if anyone actually has used it, can you tell me which is better to use? Simply var or const?
Thanks!!
Hello all,
I have a simple question. There is this keyword const in javascript, has anyone ever used it? I had never had to use it so if anyone actually has used it, can you tell me which is better to use? Simply var or const?
Thanks!!
I'd never heard of such a thing and thought it might be real useful for like getting the number of DOM elements that would later change so that the original number would be retained and available after those changes. But, its behavior in testing is inconsistent as far as I can tell.
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Use const if you want to be sure that it doesn't ever get changed, use var otherwise.
Try this, for example:
Code:<script type="text/javascript"> const i=4; i=3; alert(i); </script>
This is just my point, here are some results:
Opera - alerts 3
FF - alerts 4
IE gives syntax error on the line declaring the constant (this really refers to the line below that which attempts to change it).
As M. Gandhi said of Western Civilization:
"I think it would be a good idea."
Note: If you remove the error line in IE, it doesn't even give an alert.
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
const is a Javascript 1.7 addition, if I remember correctly, so only Spidermonkey (and possibly Rhino) have implemented it.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
I saw it listed as a part of 2.0 but, that doesn't mean it wasn't implemented before that. FF got it right in any case except if it was written as:
Which is supposed to be a way to define the data type. Done that way, FF threw an error.Code:const i : int = 3
Bottom line is, I think:
Only useful for compiled jscript (jscript.net) and server side code where supported as, on the client side, support is still too inconsistent.
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
What's the point of the const keyword? Are int and bool keywords too?
- Mike
int and bool are not currently keywords, but they are reserved for future use.
const causes a variable to be unchangeable. No error is thrown, assignment operations are simply not carried out on that variable.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Try IE: As John said, it gives an error (typical of IE).No error is thrown
I think it shouldn't be tampered with though, that they should leave it at "var". It can be assigned boolean, integer, and string values, it's pretty much the ultimate keyword. If they add the other keywords it could soon look alot like C++...
- Mike
I was talking about interpreters that support it. Perhaps I should have said:Try IE: As John said, it gives an error (typical of IE).const should cause a variable to be unchangeable. No error should be thrown, assignment operations should simply not be carried out on that variable.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Bookmarks