(no subject)
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

(no subject)

From: VK <schools_ring@yahoo.com>
Date: Tue Jul 19 2005 - 10:50:49 CEST

> > <sample>
> > var arrayObject = new Array();
> > alert(arrayObject.slice) // native code
> > arrayObject = document.getElementsByTagName('P');
> > alert(arrayObject.slice) // undefined !
>
> The getElementsByTagName method is defined as returning an object
> implementing the NodeList interface. The NodeList interface defines no
> slice method.

Exactly my point. As getElementsByTagName() returns an HTMLCollection,
JavaScript automatically converts arrayObject from Array type to
HTMLCollection type to accomodate new data structure. (You can easy
observe it by checking method/properties "before" and "after"). So it
is effectively switching interfaces for data, and this is what actually
"casting" is. Simply in typed languages casting doesn't happen "by
itself", so I call it "background casting".
Overall it's the same as

var myVar = 1; // see what methods you can apply here
    myVar = "Hello world!"; // here
    myVar = true; // and here
    // etc.

> Javascript never casts objects.
 myObject.toString();
The most primitive type of explicit casting I'm sure you used a while
already.

> The language itself only uses one type
> of object, and that object is totally dynamic so any object may
> implement any interface, and different interfaces at different times.

This description fits to any existing OOP language.
The main specifics of JavaScript that it doesn't incapsulate objects
(as well as primitive data types), so any second they are ready to be
whatever you're pushing on them from the right side of the assignement.
You give them collection - it will become HTMLCollection, Array - so be
array, number - so the better. But until the next "transformation" (if
you don't like the term "casting") they are particular objects with
particular property/methods (added right to the instance, native,
inherited or prototyped).

> Thinking in
> terms of 'casting' in a language that does not have classes is likely to
> be counterproductive.

Class is simply a factory of objects with predefined behaviors.
Each object in JavaScript has its constructor property reffering to its
creator. To avoid the word "class" we could call it "object
constructor" but it would be a bizarre term. Just say it: "class
constructor", and you will feel a gread relieve in your mind ;-)
Received on Tue Oct 18 02:58:02 2005