By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,189 Members | 1,559 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,189 IT Pros & Developers. It's quick & easy.

What does this script means?

P: n/a
this.menus = { root: new Array };

Aug 2 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
this.menus = { root: new Array };

"this" keyword can only be used when within an object. If you just
write it in the global part of the script, it won't do anything.

Also, to create a new array, you must specify arguments:
new Array(10)// new array of size 10
new Array("John Cleese","Terry Gillam","A Fish")

Try copying this to a web page:

<script>

this.menus = {root: "hello";}// does nothing - rather, causes an error

window.message = "somewhere"; // this assigns a message property
// to the window entity.
alert(window.message);// can display the content of the message

window.report = {message : "I'm a window", proof:"you clicked me"}
/*
Assigns a report property to the window object. It can be accessed via
window.report

report has two properties of its own. There are multiple ways of
accessing properties:

alert( window.report.message );// as a property in dot-notation
alert( window.report['proof'] );// as an property in a property array

The following will even work:
*/

alert(window['report']['message']);

function Person(x,y) {
// the this keyword will only work usefully if Person is instantiated
this.name = x;
this.age = y;
}

// so instantiate
dave = new Person("David",20);

alert(dave['name']);
alert(dave.age);

// this will just cause "Person" to be printed
Person("me",50);
alert(Person);

//So to finally come back to your example:

function Thingy() {
this.menus = {root: new Array(10);}// I suppose you know about arrays?
}

menuthing = new Thingy();
menuthing.menus.root[0] = "element 1";
menuthing.menus.root[1] = "element 2";
menuthing.menus.root[2] = "element 3";
// and so on...

// note that the following will NOT work:
menuthing.menus.root.4 = "something"; // that is invalid

</script>

Aug 2 '06 #2

P: n/a
AndrewTK wrote:
this.menus = { root: new Array };

"this" keyword can only be used when within an object. If you just
write it in the global part of the script, it won't do anything.
In javascript the - this - keyword _always_ refers to some object, so
you can use it _anywhere_ with an absolute guarantee that it will refer
to an object, and in so far as any reference to an object can be said
to "do" something, it will do it.
Also, to create a new array, you must specify arguments:
No, it is perfectly valid to use a function reference as the operand
for the - new - operator without any arguments (ECMA 262, 3ed Ed.
Section 11.2.2, second production).

<snip>
Try copying this to a web page:

<script>

this.menus = {root: "hello";}// does nothing - rather, causes an error
But that error is the syntax error that you introduced by moving the
semicolon from the end of the statement to just after the final
name/value pair in the Object initialiser.

With the semicolon in a more sensible location that statement (executed
in the global context) adds a property to the global object called
'menus' (if one had not previously been created) and assigns a
reference to an object to that property (the object referred to having
a property named 'root' that has the string value "hello" (and an empty
Array in the original code).

<snip>
alert( window.report.message );// as a property in dot-notation
alert( window.report['proof'] );// as an property in a property array
<snip>

That is not "as an property in a property array", it is a
bracket-notation property accessor, in the same way as the first is a
dot-notation property accessor. Javascript has no special array
accessing syntax.
<snip>

Richard.

Aug 2 '06 #3

P: n/a

Richard Cornford wrote:
In javascript the - this - keyword _always_ refers to some object, so
you can use it _anywhere_ with an absolute guarantee that it will refer
to an object, and in so far as any reference to an object can be said
to "do" something, it will do it.
Effectively it does, you are quite right.

<script>
this.foo = new Array
</script>

Will add a new array object to the object "window" under the name foo,
to answer the OP's question then.
Also, to create a new array, you must specify arguments:

No, it is perfectly valid to use a function reference as the operand
for the - new - operator without any arguments (ECMA 262, 3ed Ed.
Section 11.2.2, second production).
True, I forgot. What I meant was that brackets were needed. Through a
little test I see that that too is a mistake. The syntax was perfectly
legal.

My bad.
this.menus = {root: "hello";}// does nothing - rather, causes an error

But that error is the syntax error that you introduced by moving the
semicolon from the end of the statement to just after the final
name/value pair in the Object initialiser.
I should have seen that (!)
alert( window.report.message );// as a property in dot-notation
alert( window.report['proof'] );// as an property in a property array
That is not "as an property in a property array", it is a
bracket-notation property accessor, in the same way as the first is a
dot-notation property accessor. Javascript has no special array
accessing syntax.
I'll argue that one. As I underlined in the text that followed, it
mattered not what notation the properties were accessed - dot-notation
or square-brakcet-notation

Granted, there is no separate array for properties, it's a question of
syntax rather than a question of what-is, but saying that there is "no
array accessing syntax" is confusing (at least, it would be to me) and
I'll admit my phrasing was scant better.

For the sake of semantics, I'd rather phrase the fact as "properties
can be accessed either by property syntax (dot notation, like for C
struct "properties" shall we say) or array syntax (square-bracketed
notation)", the latter provided that the property name is valid, ie
must start with a letter or underscore - names may contain digits, but
must never start with one. Unless that too is inherently flawed?

So yes, many mistakes in my first post, thank you for correcting them
Richard. But that's the thing with the over-flexible non-compiled
language that is JavaScript - even if you write something that should
make little sense, the browser will still try to make sense of it...

Aug 3 '06 #4

P: n/a
On 03/08/2006 14:03, AndrewTK wrote:
Richard Cornford wrote:
[snip]
>That is not "as an property in a property array", it is a
bracket-notation property accessor, in the same way as the first is a
dot-notation property accessor. Javascript has no special array
accessing syntax.

I'll argue that one.
There's nothing to argue: square bracket notation is not specific to
Array objects, therefore there is no special syntax for such objects.
Where the difference between square bracket notation and dot notation
becomes significant is, as you've observed, that identifiers cannot
start with digits (only Unicode letters and escape sequences, and dollar
($) and underscore (_) symbols). As such, an array index cannot be
expressed using dot notation and one must use bracket notation where any
legal expression is acceptable (and coerced to a string).

[snip]
For the sake of semantics, I'd rather phrase the fact as "properties
can be accessed either by property syntax (dot notation, like for C
struct "properties" shall we say) or array syntax (square-bracketed
notation)", the latter provided that the property name is valid, ie
must start with a letter or underscore - names may contain digits, but
must never start with one. Unless that too is inherently flawed?
It is, both in that identifiers may start with other characters (see my
description above), and "the latter", in that quoted text, was bracket
notation not dot notation. :-P
So yes, many mistakes in my first post, thank you for correcting them
Richard. But that's the thing with the over-flexible non-compiled
language that is JavaScript - even if you write something that should
make little sense, the browser will still try to make sense of it...
Nothing that has been described here is an attempt by browsers to
accommodate something that doesn't make sense. All of it is formally
described in ECMA-262. One might argue that the language itself was
designed to be flexible, but then that's a given:

A scripting language is intended for use by both professional
and non-professional programmers. To accommodate
non-professional programmers, some aspects of the language may
be somewhat less strict.
-- 4 Overview, ECMA-262

Though the intention is clear, it still takes time to understand it
properly, and a competent programmer to use the language effectively.

Mike
Aug 3 '06 #5

P: n/a
AndrewTK wrote:
Richard Cornford wrote:
<snip>
>>alert( window.report.message );// as a property in dot-notation
alert( window.report['proof'] );// as an property in a property
array
>That is not "as an property in a property array", it is a
bracket-notation property accessor, in the same way as the
first is a dot-notation property accessor. Javascript has no
special array accessing syntax.

I'll argue that one. As I underlined in the text that followed, it
mattered not what notation the properties were accessed - dot-notation
or square-brakcet-notation

Granted, there is no separate array for properties, it's a question
of syntax rather than a question of what-is, but saying that there
is "no array accessing syntax" is confusing (at least, it would be
to me) and I'll admit my phrasing was scant better.
In my experience (including reading 400000 odd javascript relate Usenet
posts) more confusion follows from the failure to appreciate that
javascript has no special array accessing syntax.
For the sake of semantics, I'd rather phrase the fact as
"properties can be accessed either by property syntax (dot
notation, like for C struct "properties" shall we say)
To discuss any technical subject effectively it is necessary that the
terms used have a specific meaning that can be learnt and understood
with certainty by anyone wishing to discuss the subject. As there is a
technical specification for javascript, and it precisely defines all the
conceptual units necessary to explain the behaviour of javascript, the
terminology used in the specification is the clearest basis for the
discussion of the language. Anyone can go to the specification and gain
an absolute understanding of the meaning of the terminology that it
uses.

Making up terminology like "property syntax" may seem 'friendly' but it
immediately becomes open to interpretation, while a term like "property
accessor" can be cross-referenced against ECMA 262, 3rd Ed. Section
11.2.1 and its meaning pinned down to a specific syntax production and
an algorithm defining its precise behaviour.
or array syntax (square-bracketed notation)",
There is not special array accessing syntax in javascript. Mixing
bracket notation property accessors up with array access leads to
misconceptions. You get instances of people creating Array objects and
then giving them only named (non-'array index') properties, and then
labelling the result an "associative array". While the fact that no
'array index' property names were being used should have suggested to
the use of an Object object rather than an Array, and calling any
javascript object an "associative array" is misleading (because the
associative array concept suggests that you can only get values out if
you first put them in, which is not the case with any javascript Object,
and many "associative arrays" have an active length property). There is
a reason all this Array related confusion got into these situations, and
it was that the individuals responsible did not have the implications of
(and often the fact of) javascript's lack of a special array accessing
syntax driven home for them.
the latter provided that the property name is valid,
The "latter" is bracket notation, where the property name string may be
any sequence of zero or more characters.
ie must start with a letter or underscore - names
Technically, the syntax unit to which this restriction (with
corrections) applies is the "Identifier". The "names" of properties is
as unrestricted as the set of values that may be used in the brackets of
a bracket notation property accessor (i.e. no restrictions apply beyond
those imposed by factors such as available memory limiting the maximum
length of the string that could be used).
may contain digits, but must never start with one.
Unless that too is inherently flawed?

So yes, many mistakes in my first post, thank you for correcting
them Richard. But that's the thing with the over-flexible
non-compiled language that is JavaScript - even if you write
something that should make little sense, the browser will still
try to make sense of it...
Javascript is a programming language, its behaviour is precisely defined
and browsers will slavishly do precisely what has been programmed. Any
perception of "over-flexible" will only be manifestations of a lack of
precision in the understanding of the language possessed by the
programmer (possibly the result of having things explained to them in
less than precise terms).

Richard.
Aug 3 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.