How to list the statement of all var ?

T

Thierry Loiseau

Hello all,

and Happy end year 2005 !

Well, I would like to obtain a list of all JavaScript var statement,
With "for...in" perharps ?

That is bellow my recent test here, but the problem is to management
theses :-((( I must to declare and use all variable with this scheme :

v['name']

Thanx for all suggestion !

========================================================================
<html>
<head>
<title>for...in (var)</title>
</head>
<body>
<script type="text/javascript">
<!--
var v=new Array();
v['titi']="";
v['bleah']='toto';
v['toto']='bibi';
v['tutu']=109;
v['annonce_fr']="Bonne f&ecirc;tes de fin d'ann&eacute;e !";
v['annonce_en']="Happy new year, this is the end of the year 2005...
Hello 2006 !!";
function print_all() {
for (var i in window.v) {
v['titi']=(typeof(window.v)=="string")?" ' ":"";
document.write(i + " = " + v['titi'] +
"<span style='color: blue; font-weight:bold;'>"+window.v+"</span>"
+
v['titi'] + "<br>\n");
}
}
print_all();
document.write("<hr> v['tutu']+2 = "+(v['tutu']+2));
// -->
</script>
</body>
</html>
========================================================================

Thierry
and best regards from France,

<http://www.google.com/maps?ll=45.128319,1.306686&spn=0.005925,0.013087&
t=k&hl=fr>

PS : sorry for my bad english spoken :-(
 
M

Martin Honnen

Thierry Loiseau wrote:

Well, I would like to obtain a list of all JavaScript var statement,
With "for...in" perharps ?

That is bellow my recent test here, but the problem is to management
theses :-((( I must to declare and use all variable with this scheme :

v['name']

The var statement declares a variable (or more) and might additionally
initialize variables e.g.
var varName;
or
var varName = 'Kibology';

v['name'] is a property access but obviously no var statement.

The language itself does not have a "Code object model" which would
allow your script code to access the code itself in a structured way
where you could look for statements or statements of a certain type.

But global variables declared with the var statement will become
properties of the global object (which is the window object in
client-side browser script) and properties of an object can be
enumerated using the for..in statement if those properties are not
marked with the internal attribute DontEnum. As the var statement
definition does not specify that that internal DontEnum attribute should
be set for declared variables being created as properties an
implementation should make all those declared variables enumerable with
for..in.
 
T

Thierry Loiseau

Martin Honnen said:
The var statement declares a variable (or more) and might additionally
initialize variables e.g.

I'm shure "statement" is no well come in my first message :-(((
In french, "Etat" ?

Thierry
 
M

Martin Honnen

Thierry said:
I'm shure "statement" is no well come in my first message :-(((
In french, "Etat" ?

Sorry, I can't help, there is a French group fr.comp.lang.javascript as
far as I know where French is the main language, posting and reading
there might be more successful for you.
Google has it here:
<http://groups.google.com/group/fr.comp.lang.javascript?hl=en>
Hmm, I see you already post there, if that really does not help and you
want to post here then consider making a post with two languages,
English and French, here, that might then increase your chances that
someone here understands what you are looking for as you might have
someone read here who is fluent (enough) in French.
 
V

VK

Thierry said:
I'm shure "statement" is no well come in my first message :-(((
In french, "Etat" ?

"a state" - but this is a too vague term for the task description.
Based on the posted code you're trying to find either "valeur" (a
value) or "typage" (a type) - or both - of the v elements. Could you
clarify please?
( Any recognizable version of the Shakespeare language goes just fine
;-)
 
T

Thierry Loiseau

VK said:
"a state" - but this is a too vague term for the task description.

Right ! Ok !
Based on the posted code you're trying to find either "valeur" (a
value) or "typage" (a type) - or both - of the v elements. Could you
clarify please?
( Any recognizable version of the Shakespeare language goes just fine
;-)


Ok. Thank you for your post. Than my example, I would like to get value
of each !

With my script =

========================================================================
<html>
<head>
<title>for...in (var)</title>
</head>
<body>
<script type="text/javascript">
<!--
var v=new Array();
v['titi']="";
v['bleah']='toto';
v['toto']='bibi';
v['tutu']=109;
v['annonce_fr']="Bonne nouvelle ann&eacute;e 2006 !!";
v['annonce_en']="Happy new year 2006 !!";
function print_all() {
for (var i in window.v) {
v['titi']=(typeof(window.v)=="string")?" ' ":"";
document.write(i + " = " + v['titi'] +
"<span style='color: blue; font-weight:bold;'>"+window.v+"</span>"
+
v['titi'] + "<br>\n");
}
}
print_all();
document.write("<hr> v['tutu']+2 = "+(v['tutu']+2));
// -->
</script>
</body>
</html>
========================================================================

The result is :

========================================================================

titi = ' ' '
bleah = ' toto '
toto = ' bibi '
tutu = 109
annonce_fr = ' Bonne nouvelle année 2006 !! '
annonce_en = ' Happy new year 2006 !! '
v['tutu']+2 = 111
========================================================================

I repeat : the problem is to management theses :-(((
So, I must to declare and use all variable with this strict scheme
(syntax, scheme is a good word ?) :

v['name']

Thanx for all suggestion !

Thierry,
best regards from France
 
T

Thierry Loiseau

VK said:
"a state" - but this is a too vague term for the task description.

Right ! Ok !
Based on the posted code you're trying to find either "valeur" (a
value) or "typage" (a type) - or both - of the v elements. Could you
clarify please?
( Any recognizable version of the Shakespeare language goes just fine
;-)


Ok. Thank you for your post. Than my example, I would like to get value
of each !

With my script =

========================================================================
<html>
<head>
<title>for...in (var)</title>
</head>
<body>
<script type="text/javascript">
<!--
var v=new Array();
v['titi']="";
v['bleah']='toto';
v['toto']='bibi';
v['tutu']=109;
v['annonce_fr']="Bonne nouvelle ann&eacute;e 2006 !!";
v['annonce_en']="Happy new year 2006 !!";
function print_all() {
for (var i in window.v) {
v['titi']=(typeof(window.v)=="string")?" ' ":"";
document.write(i + " = " + v['titi'] +
"<span style='color: blue; font-weight:bold;'>"+window.v+"</span>"
+ v['titi'] + " [" + typeof(window.v) + "]<br>\n");
}
}
print_all();
document.write("<hr> v['tutu']+2 = "+(v['tutu']+2));
// -->
</script>
</body>
</html>
========================================================================

The result is :

========================================================================

titi = ' ' '
bleah = ' toto '
toto = ' bibi '
tutu = 109
annonce_fr = ' Bonne nouvelle année 2006 !! '
annonce_en = ' Happy new year 2006 !! '
v['tutu']+2 = 111
========================================================================

I repeat : the problem is to management theses :-(((
So, I must to declare and use all variable with this strict scheme
(syntax, scheme is a good word ?) :

v['name']

Thanx for all suggestion !

Thierry,
best regards from France
 
T

Thierry Loiseau

VK said:
"a state" - but this is a too vague term for the task description.

Right ! Ok !
Based on the posted code you're trying to find either "valeur" (a
value) or "typage" (a type) - or both - of the v elements. Could you
clarify please?
( Any recognizable version of the Shakespeare language goes just fine
;-)


Ok. Thank you for your post. Than my example, I would like to get value
of each !

With my script =

========================================================================
<html>
<head>
<title>for...in (var)</title>
</head>
<body>
<script type="text/javascript">
<!--
var v=new Array();
v['titi']="";
v['bleah']='toto';
v['toto']='bibi';
v['tutu']=109;
v['annonce_fr']="Bonne nouvelle ann&eacute;e 2006 !!";
v['annonce_en']="Happy new year 2006 !!";
function print_all() {
for (var i in window.v) {
v['titi']=(typeof(window.v)=="string")?" ' ":"";
document.write(i + " = " + v['titi'] +
"<span style='color: blue; font-weight:bold;'>"+window.v+"</span>"
+ v['titi'] + " [" + typeof(window.v) + "]<br>\n");
}
}
print_all();
document.write("<hr> v['tutu']+2 = "+(v['tutu']+2));
// -->
</script>
</body>
</html>
========================================================================

The result print in the page is :

========================================================================
titi = ' ' ' [string]
bleah = ' toto ' [string]
toto = ' bibi ' [string]
tutu = 109 [number]
annonce_fr = ' Bonne nouvelle année 2006 !! ' [string]
annonce_en = ' Happy new year 2006 !! ' [string]
v['tutu']+2 = 111
========================================================================

I repeat : the problem is to management theses :-(((
So, I must to declare and use all variable with this strict scheme
(syntax, scheme is a good word ?) :

v['name']

Thanx for all suggestion !

Thierry,
best regards from France
 
V

VK

Thierry said:
I repeat : the problem is to management theses :-(((
So, I must to declare and use all variable with this strict scheme
(syntax, scheme is a good word ?) :

v['name']

Array ("rangee") consists of arrayObject.length elements and you handle
them by their index which is positive integer:
v[10] = 'foo';
alert(v[10]);

The posted structure is *not* an array. So proper way to handle it
would be (one of possibilities):

var v=new Object(); // not Array!
v['titi']="";
v['bleah']='toto';
v['toto']='bibi';
v['tutu']=109;
v['annonce_fr']="Bonne nouvelle ann&eacute;e 2006 !!";
v['annonce_en']="Happy new year 2006 !!";
etc. as you already do

And of course no one stops you from declaring separate variables which
are not parts of any higher structure:

var blah_blah_blah = "blah-blah-blah";

I guess you need to find a good French manual of JavaScript (I cannot
advise any).
For English reading you may visit:
<http://www.geocities.com/schools_ring/ArrayAndHash.html>



:-D ;-)
 
T

Thierry Loiseau

VK said:
Thierry said:
I repeat : the problem is to management theses :-(((
So, I must to declare and use all variable with this strict scheme
(syntax, scheme is a good word ?) :

v['name']

Array ("rangee") consists of arrayObject.length elements and you handle
them by their index which is positive integer:
v[10] = 'foo';
alert(v[10]);

The posted structure is *not* an array. So proper way to handle it
would be (one of possibilities):

var v=new Object(); // not Array!

Ok !
v['titi']="";
v['bleah']='toto';
v['toto']='bibi';
v['tutu']=109;
v['annonce_fr']="Bonne nouvelle ann&eacute;e 2006 !!";
v['annonce_en']="Happy new year 2006 !!";
etc. as you already do

And of course no one stops you from declaring separate variables which
are not parts of any higher structure:

var blah_blah_blah = "blah-blah-blah";

I guess you need to find a good French manual of JavaScript (I cannot
advise any).
For English reading you may visit:
<http://www.geocities.com/schools_ring/ArrayAndHash.html>

Ok (too). So I make a long script to my (sorry in french) personal
homepage.


Yep !

Thanx VK !

Thierry Loiseau
and Best regards
From France
 
T

Thierry Loiseau

VK said:
Thierry said:
I repeat : the problem is to management theses :-(((
So, I must to declare and use all variable with this strict scheme
(syntax, scheme is a good word ?) :

v['name']

Array ("rangee") consists of arrayObject.length elements and you handle
them by their index which is positive integer:
v[10] = 'foo';
alert(v[10]);

The posted structure is *not* an array. So proper way to handle it
would be (one of possibilities):

var v=new Object(); // not Array!

Ok ! But my script run with a good result, no ?
v['titi']="";
v['bleah']='toto';
v['toto']='bibi';
v['tutu']=109;
v['annonce_fr']="Bonne nouvelle ann&eacute;e 2006 !!";
v['annonce_en']="Happy new year 2006 !!";
etc. as you already do

And of course no one stops you from declaring separate variables which
are not parts of any higher structure:

var blah_blah_blah = "blah-blah-blah";

I guess you need to find a good French manual of JavaScript (I cannot
advise any).
For English reading you may visit:
<http://www.geocities.com/schools_ring/ArrayAndHash.html>

Ok (too). So I make a long script to my (sorry in french) personal
homepage.


Yep !

Thanx VK !

Thierry Loiseau
and Best regards
From France
 
T

Thierry Loiseau

VK said:
Cool! So does everything work as expected now?

From <http://astrophoto.free.fr/calculs/>
=> clic on button "simulateur copernic" and after onload, clic on
"astrophoto"... :)))
=> clic on button "Lunes de Jupiter" and clic on the item "Repères", on
left, top, twice to view animation...

=> clic on button "visibilité des planètes", clic on the image-planete
then, there is a popup, and you see the position of this in the sky.
=> clic on button "astrophoto" to see the constelllation...
Smilies were not to you directly, just a... conditional signature :)

Argh, ok !-)

Best regards from France,

Thierry

PS : VK, do you speak french ?
 
R

RobG

Thierry said:
Ok ! But my script run with a good result, no ?

Because Arrays are Objects, so you can do with them everything that
you can do with ordinary objects.

Arrays are a special objects with a property 'length' and a number of
special methods - splice, push, pop, concat et cetera - but otherwise
they are just like other objects.

VK suggested using an ordinary object because you weren't using the
special property or methods of an array.


[...]
 
V

VK

Thierry said:
Ok ! But my script run with a good result, no ?

It does... until when... because any JavaScript/JScript objext extends
Object constructor.

By the usage twist it is similar to:

function f1() {
with (arguments.callee) {
return arg1 + arg2;
}
}

f1.arg1 = 2;
f1.arg2 = 2;
alert(f1());

Here too you're declaring *function* f1 but overloading it as a
primitive Object. JavaScript is flexible enough for such and even more
weird things, but not would it be better to use the conventional way?

function f1(arg1, arg2) {
return arg1+arg2;
}

alert(f1(2,2));

You need to use hacks only when you have too. Otherwise you should use
the things with the intended purposes and in the intended way.
IMHighlyHO
 
V

VK

Thierry said:
Ok ! But my script run with a good result, no ?

It does... until when... because any JavaScript/JScript objext extends
Object constructor.

So by the usage twist it is similar to:

function f1() {
with (arguments.callee) {
return arg1 + arg2;
}
}

f1.arg1 = 2;
f1.arg2 = 2;
alert(f1());

Here too you're declaring a "Function* object but using it as an Object
object. JavaScript is flexible enough for even more tricky things. But
wouldn't it be better to use the conventional way:

function f1(arg1, arg2) {
return arg1 + arg2;
}

alert(f1(2,2));

You should use hacks only if you have too otherwise you should use
things in the conventional way with the conventional purpose.
IMHighlyHO
 
T

Thomas 'PointedEars' Lahn

VK said:
It does... until when... because any JavaScript/JScript objext extends
Object constructor.

Wrong. What you maybe meant to say was that user-defined objects
inherit properties from the Object prototype which would be disabled
for an inheriting object as long as their name would be used as name
of a property of that object.
By the usage twist it is similar to:

function f1() {
with (arguments.callee) {
return arg1 + arg2;
}
}

f1.arg1 = 2;
f1.arg2 = 2;
alert(f1());

Here too you're declaring *function* f1 but overloading it as a
primitive Object.

Nothing is "overloaded" here. As in functional programming languages,
functions _are_ (first-class) objects in J(ava)Script/ECMAScript:
Function objects. Neither objects nor references are primitive values.


PointedEars
 
Z

zwetan

Hello,
Well, I would like to obtain a list of all JavaScript var statement,
With "for...in" perharps ?

That is bellow my recent test here, but the problem is to management
theses :-((( I must to declare and use all variable with this scheme :

v['name']

Thanx for all suggestion !
[snip]

If you want to trace into an associative Array, use an object Object instead

in FF for ex:
---------------
var v=new Object();
v['titi']="";
v['bleah']='toto';
v['toto']='bibi';
v['tutu']=109;
v['annonce_fr']="Bonne f&ecirc;tes de fin d'ann&eacute;e !";
v['annonce_en']="Happy new year, this is the end of the year 2005...Hello
2006 !!";

document.write( "v:"+v.toSource() );
---------------

will trace
---------------
v:({titi:"", bleah:"toto", toto:"bibi", tutu:109, annonce_fr:"Bonne fêtes
de fin d'année !", annonce_en:"Happy new year, this is the end of the year
2005...Hello 2006 !!"})
---------------


if you want the same functionality in any browser you can also use my core2
library
http://www.burrrn.com/projects/core2.html

here an exemple
---------------
<html>
<body>
<head>
<script type="text/javascript" src="core2_v1.0.0_JS.js"></script>

<style type="text/css">

html
{
height: 100%;
overflow: hidden;
}

body
{
height: 100%;
margin: 0;
padding: 0;
background-color: #FFFFFF;
}

#TraceConsole
{
width: 600px;
/* height: 400%; */
text-align: left;
font-size: 12px;
/* font-family: monospace; */
/* font-family: tahoma, Verdana, Helvetica, Sans-Serif; */
font-family: courier,fixed,swiss,sans-serif;
border: 1px dotted #cccccc;
display: block;
}

</style>
</head>
<title>blah</title>
<html>
<div id="TraceConsole"></div>
<script type="text/javascript">

var v=new Object();
v['titi']="";
v['bleah']='toto';
v['toto']='bibi';
v['tutu']=109;
v['annonce_fr']="Bonne f&ecirc;tes de fin d'ann&eacute;e !";
v['annonce_en']="Happy new year, this is the end of the year 2005...Hello
2006 !!";

trace( "v:"+v.toSource( 0 ) );

</script>

</body>
</html>
---------------

will trace in the DIV
---------------
v:
{
titi:"",
bleah:"toto",
toto:"bibi",
tutu:109,
annonce_fr:"Bonne fêtes de fin d\'année !",
annonce_en:"Happy new year, this is the end of the year 2005...Hello
2006 !!"
}
---------------

Alternatively you could also use another way of organizing your local
strings

for exemple:
---------------
var AnnounceStrings = new Object();
AnnounceStrings.fr = new Object();
AnnounceStrings.en = new Object();

AnnounceStrings.fr.happyNewyear = "Bonne f&ecirc;tes de fin d'ann&eacute;e
!";
AnnounceStrings.fr.hello = "bonjour {0}";

AnnounceStrings.en.happyNewyear ="Happy new year, this is the end of the
year 2005...Hello 2006 !!";
AnnounceStrings.en.hello = "hello {0}";

trace( "AnnounceStrings:"+AnnounceStrings.toSource( 0 ) );

var userName = "toto";
var lang = "fr";

trace( "--------" );
trace( String.format( AnnounceStrings[ lang ].hello, userName ) );
---------------


will trace in the DIV
---------------
AnnounceStrings:
{
fr:
{
happyNewyear:"Bonne fêtes de fin d\'année !",
hello:"bonjour {0}"
},
en:
{
happyNewyear:"Happy new year, this is the end of the year
2005...Hello 2006 !!",
hello:"hello {0}"
}
}
 
T

Thomas 'PointedEars' Lahn

Please provide attribution of quoted material:

That is bellow my recent test here, but the problem is to management
theses :-((( I must to declare and use all variable with this scheme :

v['name']
[...]
[snip]

If you want to trace into an associative Array, use an object Object
instead

Probably you meant to say "There are no associative Arrays in JS/ECMAScript.
What is achieved with associative arrays in other languages, is provided by
properties of objects and property accessors here."
in FF for ex:
---------------
var v=new Object();
v['titi']="";
v['bleah']='toto';
v['toto']='bibi';
v['tutu']=109;
v['annonce_fr']="Bonne f&ecirc;tes de fin d'ann&eacute;e !";
v['annonce_en']="Happy new year, this is the end of the year 2005...Hello
2006 !!";

document.write( "v:"+v.toSource() );
---------------

will trace
---------------
v:({titi:"", bleah:"toto", toto:"bibi", tutu:109, annonce_fr:"Bonne fêtes
de fin d'année !", annonce_en:"Happy new year, this is the end of the year
2005...Hello 2006 !!"})
---------------

No, it will not always, unless toSource() is user-defined.
if you want the same functionality in any browser

You meant to say "with any script engine" but even that would not be true.
you can also use my
core2 library
http://www.burrrn.com/projects/core2.html

here an exemple

Invalid examples are worthless. said:
will trace in the DIV

IMHO, "tracing" is following traces, such as a stack trace where one call
on the stack leads to the next. So I do not see any tracing here, let alone
the "core2" library fulfilling its self-imposed requirement of "same API
everywhere (JavaScript, JScript, ActionScript, etc.)", considering e.g.
the untested use of document.getElementById() and proprietary `.innerHTML'.
I also see other examples of bad code style: an undeclared variable just to
call a function and then `delete' it, bad indentation, error-prone property
inference and so on. Certainly something that will make your day
interesting.


PointedEars
___________
[1] See <URL:http://en.wikipedia.org/wiki/Trace>, Computing, which you often
like to refer to on your Web site.
 
V

VK

Thomas 'PointedEars' Lahn wrote:
Wrong. What you maybe meant to say was that user-defined objects
inherit properties from the Object prototype which would be disabled
for an inheriting object as long as their name would be used as name
of a property of that object.

Programming entities: Evolutionism or Creationism? :)

I'm staing exclusively on the Evolutionism position here. There is the
initial protozoa Object() and everything else are branches of evolution
from this starting point. Date(), String(), Array(), myObject(),
myOtherObject() are all equally coming that protozoa despite their
evolution branches (thus property sets) can be twisted in the most
amazing ways.

Therefore arguments like "Array() is almost like Object() so no big
deal what to use" to me sounds like "daschund is really same kind as
buldog so no big deal what to buy".

I accept the possibility of other theories including the promoted
Selective Creationism, there all things are by themselves, and some
things should be considered as one entity (despite some differences)
because they've been created by Them to be one thing.

I still believe that my God is better :)
Nothing is "overloaded" here.
I guess my usage of "overload" is not canonical. For me it's a way to
use the "other side" of things. Like create Array() and use it as
associative array via Object() side. Or store Function() arguments in
its Object() background (like in my sample), or force plus to works as
minus - something like this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top