> // this is the same as var Test = function() ...
> // or this.Test = function() ...
> function Test() {
On Jan 30, 10:32 am, "Nickolay Ponomarev" <asquee...@gmail.com>
wrote:
> [...]
> Any reasons you don't link to the migrated reference on
> developer.mozilla.org instead? The devedge-temp is just a temporary
> mirror of old devedge content, it's not maintained and is not
> guaranteed to exist..
Well, no reason, really, it's just the first link that Google
returned :) Since I have no need to keep such basic documentation
around, I simply did a simple Google and returned the first context-
related entry. Sorry if there are better references.
> Unlike Java, there is not concept of 'class' in JS yet :)
True. I meant "Object". Though, technically, even if the concept of
'class' is not present (documented) in the language, using 'new' to
call function consctructors is in definition creating a new instance
of a custom object. Isn't that also the definition of creating an
instance of a custom class ? I may be misitaking, but the ressemblance
is pretty close. Though I agree to say that it is wrong to call
'class' what is not truly one.
Also, perhaps my example did lack of precision, whenever a 'var'
instruction is found in a script, even if further down the actual
executed line, that variable will be undefined (this is why there
cannot be two 'var test;' declaration in the same context) So, by
removing the 'var test;' declaration in the function printTest, the
first alert( test ); would print {Object: object} ; it is, in fact,
pointing to the global 'test' because no local variable of that name
is found locally, so it goes 'up one context'. (if I may say)
Despite it all, my point of view still stands ; only fools bind every
declarations to the window object.
-Yanick
Richard Cornford - 30 Jan 2007 22:53 GMT
<snip>
>> Unlike Java, there is not concept of 'class' in JS yet :)
And with continuing good fortune there never will be.
> True. I meant "Object". Though, technically, even if the
> concept of 'class' is not present (documented) in the
> language, using 'new' to call function consctructors is
> in definition creating a new instance of a custom object.
> Isn't that also the definition of creating an instance
> of a custom class ?
Not really. In a class based language a class is fixed by its definition
before the code executes, and all object instances created with a
constructor are or the same 'type' (in the sense that they all have the
same methods and properties/fields/members). In javascript using - new -
with a function does not guarantee that each object created will have the
same properties and methods, and once created any object can have its
properties and methods swapped/modified/removed.
It is still possible to implement the 'class' concept in javascript (and
there are many more ways of instantiating object of a 'class' than just
using - new - on a constructor function), but in order to employ the
concept you have to treat the 'class' definition as fixed and the object
instances employed as immutable (in form, not with regard to the data
they hold) (not at all difficult as you just don't set about modifying
them at run time).
It is not at all difficult to employ the 'class' concept when designing
and implementing javascript programs, but the bottom line is that such a
concept is imposed upon the resulting code by the programmer and is not
part of the language. And it is useful to remember where these things are
coming from, not least because that makes it easier to see that there are
many more choices available, and so decisions to make, when using
javascript than are on offer in class based languages.
> I may be misitaking, but the ressemblance is pretty close.
> Though I agree to say that it is wrong to call 'class' what
> is not truly one.
If code implements the 'class' concept (by design) then I have no trouble
referring to the result as a class.
> Also, perhaps my example did lack of precision, whenever a 'var'
> instruction is found in a script, even if further down the actual
> executed line, that variable will be undefined (this is why there
> cannot be two 'var test;' declaration in the same context)
<snip>
There can be as many - var test; - statements within the same context as
you like. There is little point in having more than one, but the language
allows more and is very clear on how they will be handled.
It is even not uncommon to see the same Identifier declared as a counter
in multiple - for - statements within the same function. I am not
particularly in favour of that but reasonable justifications have been
presented for its being a good (or at least valid) practice.
Richard.
Nickolay Ponomarev - 30 Jan 2007 23:42 GMT
> On Jan 30, 10:32 am, "Nickolay Ponomarev" <asquee...@gmail.com>
> wrote:
[quoted text clipped - 7 lines]
> is pretty close. Though I agree to say that it is wrong to call
> 'class' what is not truly one.
I was just nit-picking, your example was good. When comparing JS to
Java it's better to avoid mentioning JS classes to avoid unnecessary
confusion :)
Nickolay