Re: What does this script means?




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...

.



Relevant Pages

  • Re: .join() !== + a + b; // for some a and b
    ... The one seemed obvious until your more detailed explanation below, ... that made it necessary to look deeper into the syntax. ... one an Array object reference and one ... seems wrong with the expression evaluation on the left side of the logical ...
    (comp.lang.javascript)
  • Re: Another spinoza challenge
    ... point with an array rather than a hypothetical struct syntax: ...   byref; ... The only test it fails is that C does not have pass by reference! ...
    (comp.lang.c)
  • Re: Cant pass dynamic arrays to procedure without EInvalidPointer exception
    ... If the syntax was ... I much more often want to reference every element in an array with ... an index type cardinality much greater than the array length. ...
    (comp.lang.pascal.delphi.misc)
  • Re: How to do "Compile time" initialization of record array?
    ... It should be noted that there is proposed syntax for C# 3.0 which will ... public struct MyStruct ... What actually gets compiled is the same array initialization as there is ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: It Hurts When I Do This
    ... We do have a syntax that means the entire array - the array name. ... I disagree that it helps to use a special notation to indicate that ... I think it a mistake to throw all this kind of thing in the language. ...
    (comp.lang.fortran)