473,520 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ 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 26266
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
1570
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
8830
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 to it. It would sure be nice to declare the receiving routine to be something like: void DoSomething(const ref HugeThing) { ... }
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 respond with an error upon seeing 'const'? In pseudo-code: try_this { const SOME_CONSTANT = 0,
20
2414
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 doesn't write to x, then it doesnt seem like the compiler could care less whether I specify the const part. Quite the opposite, if one uses const...
41
2324
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
13616
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, hash<const char*>, eqstrmonths; months = 31; I think it is for the use in calls like function("my string"), but, is it really necessary to define...
23
2299
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
1407
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 because all 3 ways of naming look bad and/or hard to type. From what I understand, scheme can have variables like var-name. I'm curious about...
10
5928
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 is unused, I know that it does not take up storage in the
0
7299
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7201
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7602
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7559
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4788
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3282
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1646
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 we have to send another system
0
506
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.