Re: What does this script means?
- From: "AndrewTK" <atkedzierski@xxxxxxxxx>
- Date: 3 Aug 2006 06:03:09 -0700
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...
.
- Follow-Ups:
- Re: What does this script means?
- From: Richard Cornford
- Re: What does this script means?
- From: Michael Winter
- Re: What does this script means?
- References:
- What does this script means?
- From: Cylix
- Re: What does this script means?
- From: AndrewTK
- Re: What does this script means?
- From: Richard Cornford
- What does this script means?
- Prev by Date: Re: Why not Prototype? (Was: Re: Issues with IE & Prototype/AJAX)
- Next by Date: Close popup window on session expire
- Previous by thread: Re: What does this script means?
- Next by thread: Re: What does this script means?
- Index(es):
Relevant Pages
|