473,480 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

'var' and 'const' keywords



Has anyone found it possible---that is, come up with a "trick"----to
specify IN THE SAME SCOPE 'const' for non-reassignable variables, with the
use of 'var' as a fallback should the interpreter respond with an error
upon seeing 'const'?

In pseudo-code:

try_this {

const SOME_CONSTANT = 0,
ANOTHER_CONSTANT = 1;

} or_try_this {

// re-assignable definitions despite not being wanted

var SOME_CONSTANT = 0,
ANOTHER_CONSTANT = 1;

}

Note that a try/catch block will fail presumably because they are in the
same scope, of which there are only two scopes in
ECMAScript/Javascript/JScript, as near as I can tell.

If that is asking for the existence of a logical contradiction, then what
other means is there to obtain this?
PG
Dec 28 '05 #1
5 26254
Patient Guy wrote:
Has anyone found it possible---that is, come up with a "trick"----to
specify IN THE SAME SCOPE 'const' for non-reassignable variables, with the
use of 'var' as a fallback should the interpreter respond with an error
upon seeing 'const'?

In pseudo-code:

try_this {

const SOME_CONSTANT = 0,
ANOTHER_CONSTANT = 1;


const is a future reserved word but is not a key word nor does it have
any special meaning in ECMAScript Ed 3.

It has been suggested for inclusion in the next version of ECMAScript
and JavaScript 2.0. There are some interesting links here:

<URL: http://www.mozilla.org/js/language/ >
If you want to create a variable that can't be modified, create it as
a private member of an object and that also has a privileged method
that reads it (but can't write to it):

<URL: http://www.crockford.com/javascript/private.html >
Set its value when you create the object. Not foolproof, but if you
want something more provide a scenario.
[...]
--
Rob
Dec 28 '05 #2


RobG wrote:

const is a future reserved word but is not a key word nor does it have
any special meaning in ECMAScript Ed 3.

It has been suggested for inclusion in the next version of ECMAScript
and JavaScript 2.0.


It is correct that in ECMAScript edition 3 const is only future
reserved. However Netscape's respectively now Mozilla's JavaScript
implementation for JavaScript 1.5 and later already supports const e.g.

const GOD = 'Kibo';
alert(GOD)

It is obviously not possible to use that on the web as most other
implementations will give you a syntax error but for Mozilla specific
scripting (XUL, extensions) it can be used and is used.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 28 '05 #3
VK

Patient Guy wrote:
Has anyone found it possible---that is, come up with a "trick"----to
specify IN THE SAME SCOPE 'const' for non-reassignable variables, with the
use of 'var' as a fallback should the interpreter respond with an error
upon seeing 'const'?


This is not the problem of the *execution* - it is the problem of
*pre-processing* when the source code is being parsed and broken on
tokens.

While you have plenty of control during the execution like
if(document.someMethod) etc. there is nothing you can do with
pre-processing *from within the script itself* : an absolute shiny
none. I'm saying it in case if you're currently trying to find some
"hack".
An absolute shiny none because pre-processing errors will happen
*before* the execution - thus before your script will become.

The only possibility (as in all other languages) is to use
pre-processor commands. Unfortunately up do date IE is the only browser
with an open interface pre-processor. All others still prefer to sit on
the cloud.

<script type="text/javascript">
/*@cc_on
@if (@_jscript_version >= 7)
/* JScript.Net with const support */
const foo = 'bar';
@elif (@_jscript)
/* some JScript but not JScript.Net */
var foo = 'bar';
@else @*/
/* Non-IE browser: act on your own risk */
// ?
/*@end @*/
</script>

Dec 28 '05 #4
VK wrote:
Patient Guy wrote:
Has anyone found it possible---that is, come up with a "trick"----to
specify IN THE SAME SCOPE 'const' for non-reassignable variables, with
the use of 'var' as a fallback should the interpreter respond with an
error upon seeing 'const'?
This is not the problem of the *execution* -


(That would be the interpretation of the compiled byte-code.)
it is the problem of *pre-processing* when the source code is being
parsed and broken on tokens.


That is compilation of the source code. JS/ECMAScript != PHP.
PointedEars
Dec 28 '05 #5
VK

Thomas 'PointedEars' Lahn wrote:
<snip>
(That would be the interpretation of the compiled byte-code.) <snip> That is compilation of the source code. JS/ECMAScript != PHP.


Right.

Also please note that one should avoid nested comments as it can hit
you. I used internal comments in the posted code only to... uhm...
comment the branches. On a real run keep your comments with you or
outside of the @cc block.

Dec 28 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1565
by: wtnt | last post by:
Hello. I previously had a program that compiled and worked with no error. Relevant parts here: class BasicList{ public: char* listLookup() { item = buffer; ...
17
8822
by: My Name | last post by:
There was a topic on this earlier, but no answer, only people asking "Why do you want to do this..." Let's say I have a HUGE object and want to pass it to a routine that only needs read access...
1
429
by: Patient Guy | last post by:
Has anyone found it possible---that is, come up with a "trick"----to specify IN THE SAME SCOPE 'const' for non-reassignable variables, with the use of 'var' as a fallback should the interpreter...
20
2411
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
41
2321
by: Dead Loop | last post by:
Hi all, I'm a beginner and my question is: Are there any differences between char *p = "Hello, world!"; and const char *p = "Hello, world!"; ?
14
13607
by: Javier | last post by:
Hello, in which cases is it better the use of "const char*" to "string" (or even const string &). I mean, in STL (http://www.sgi.com/tech/stl/hash_map.html) I see: hash_map<const char*, int,...
23
2295
by: Kira Yamato | last post by:
It is erroneous to think that const objects will have constant behaviors too. Consider the following snip of code: class Person { public: Person(); string get_name() const
13
1404
by: Rainy | last post by:
I have a stylistic question. In most languages words in var. name are separated by underscores or cap letters, resulting in var names like var_name, VarName and varName. I don't like that very much...
10
5923
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
0
7049
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6912
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7092
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6981
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3000
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.