Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

finding the type of an object? ("typeof" doesn't work)

6 views
Skip to first unread message

Bennett Haselton

unread,
May 1, 2004, 3:15:37 PM5/1/04
to
Is there any way to find a string representing an object's class,
which will work in Internet Explorer 6?

"typeof" doesn't work -- it returns "object" for all objects:

x = window.open('http://www.yahoo.com/');
alert(typeof x);

And I found this page:
http://www.mozilla.org/js/language/js20-2002-04/core/expressions.html
which claims: "To get the type of an object x, use x.class".

However, that doesn't work in IE 6 so it must be Mozilla-only.

Can it be done?

-Bennett

Martin Honnen

unread,
May 2, 2004, 10:47:29 AM5/2/04
to

Bennett Haselton wrote:

> Is there any way to find a string representing an object's class,
> which will work in Internet Explorer 6?
>
> "typeof" doesn't work -- it returns "object" for all objects:
>
> x = window.open('http://www.yahoo.com/');
> alert(typeof x);

Well, JavaScript 1.x doesn't have classes so I am not sure what you are
looking for. In terms of JavaScript/ECMAScript edition 3 x is of type
Object.


--

Martin Honnen
http://JavaScript.FAQTs.com/

Jim Ley

unread,
May 3, 2004, 8:13:15 AM5/3/04
to
On Sun, 02 May 2004 22:43:28 -0500, Fox <f...@fxmahoney.com> wrote:

>Normally, you would parse the constructor, but window.constructor and
>document.constructor are both "undefined" in IE (it's not JavaScript -
>it's JScript).

They're host objects, so the language is irrelevant. You get the same
behaviour in DScript, and if anyone packaged up SpiderMonkey as an
ActiveScripting language you would there to.

> Furthermore, in IE, window and document are
>synonymous (but not exactly equal to each other).
>
>proof:
>
>alert( window == document );
>
>// IE => true; Moz => false

That they get type converted to something that == each other in IE is
not proof that they are synonymous.

>Considering that you cannot instantiate a Window or Document object from
>within JavaScript, it really doesn't matter one way or the other.

Indeed!

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Thomas 'PointedEars' Lahn

unread,
May 20, 2004, 3:48:32 AM5/20/04
to
Bennett Haselton wrote:

> Is there any way to find a string representing an object's class,
> which will work in Internet Explorer 6?

No, since ECMAScript up to ed. 3 and thus JavaScript up to version 1.5
and JScript up to version 5.6 do not provide a class-based object model
but a prototype-based one.

> "typeof" doesn't work --

It does.

> it returns "object" for all objects:

As it is supposed to.

> x = window.open('http://www.yahoo.com/');

Have you declared the variable previously? If not, you better use the
`var' keyword here.

> alert(typeof x);
>
> And I found this page:
> http://www.mozilla.org/js/language/js20-2002-04/core/expressions.html
> which claims: "To get the type of an object x, use x.class".

ECMAScript 4 is yet to be standardized and JavaScript 2.0 to be the base
of it is yet to be implemented in UAs. If you would have read the above
thoroughly you would have noticed that there is still only one
implementation of ECMAScript 4 available -- Epimetheus. (Which could
become a prophetical choice since it is entirely possible that ECMAScript
4/JavaScript 2.0 will never be finished as AOLTW had temporarily closed the
Netscape browser division recently and, as probably a result, Netscape is
no longer a member of ECMA.)

> However, that doesn't work in IE 6

IE resp. the Windows Script Host supports, among other script languages
like VBScript, JScript -- Microsoft's implementation of ECMAScript up to
ed. 3. So a JavaScript spec/doc is simply the wrong place to look, no
matter how current it is.

> so it must be Mozilla-only.

It does not work in current Mozillas either.

> Can it be done?

That depends on what you are looking for and where. In ECMAScript 3 and
thus JavaScript 1.5 each object has a "constructor" property (inherited
from the Object prototype) referring to the constructor used to create the
object. Since that constructor function (in fact, *every* named function
statement) is also the definition for an object prototype, objects created
using the Foo() constructor can be referred to as "Foo objects".
window.open() returns a reference to a Window object if successful. One
could test for this in Gecko-based UAs with

if (x && x.constructor && x.constructor == Window)
{
// ...
}

But since window.open() never returns an object of a type different
from Window if successful and not every UA provides a public prototype
for all of its host objects (as Window objects are), that test appears
to be only academical. AFAIK

if (x)
{
// ...
}

always sufficed to date. If there are statements between the
window.open() call and the test, the latter should be changed to

if (x && !x.closed)
{
// ...
}

Both solutions have been pointed out to numerous times in this newsgroup
before. Please search before you post, see the FAQ.


PointedEars

Lasse Reichstein Nielsen

unread,
May 20, 2004, 7:25:46 AM5/20/04
to
Thomas 'PointedEars' Lahn <Point...@nurfuerspam.de> writes:

> Bennett Haselton wrote:
> If you would have read the above thoroughly you would have noticed
> that there is still only one implementation of ECMAScript 4
> available -- Epimetheus.

I believe JScript.NET is also a (perhaps partial) implementation of
ECMAScript v4.

> That depends on what you are looking for and where. In ECMAScript 3 and
> thus JavaScript 1.5 each object has a "constructor" property (inherited
> from the Object prototype) referring to the constructor used to create the
> object.

The constructor isn't necessarily inherited. When a user declared
function is created, its prototype object is also created and assigned
to the function's "prototype" property. After that, the function is
assigned to the prototype object's "constructor" property.
I.e.,
function foo(){};
also creates the new object
foo.prototype
and makes the assignment
foo.prototype.constuctor = foo;

In this case, the constructor property is not inherited.

In the case of host objects, all bets are off, as usual. In my
browser, Opera, it is correct that window.constuctor is inherited (it
is Object). In Mozilla, where there are available constructors for
most host objects, window.constructor is the global function Window.

> Since that constructor function (in fact, *every* named function
> statement) is also the definition for an object prototype, objects created
> using the Foo() constructor can be referred to as "Foo objects".

Yes, as long as one makes sure not to break the relationship by manually
manipulating prototypes.
---
function Foo(){ this.x = 42; }
function Bar(){ this.y = 37; };
var foo = new Foo();
var bar = new Bar();
alert([foo instanceof Foo,
foo instanceof Bar,
bar instanceof Foo,
bar instanceof Bar]); // true,false,true,false
// swap:
var fprot = Foo.prototype;
Foo.prototype = Bar.prototype;
Bar.prototype = fprot;
Foo.prototype.constructor = Foo;
Bar.prototype.constructor = Bar;
// and they are swapped.
alert([foo instanceof Foo,
foo instanceof Bar,
bar instanceof Foo,
bar instanceof Bar]); // false,true,false,true
---
The connection between an object and its constructor is really a connection
between the object and the constructor function's prototype object, because
inhertance happens between objects. The constructor functions merely
facilitate the creation and initialization of new objects based on an
old object.

/L
--
Lasse Reichstein Nielsen - l...@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Thomas 'PointedEars' Lahn

unread,
May 20, 2004, 11:33:14 AM5/20/04
to
Lasse Reichstein Nielsen wrote:

> Thomas 'PointedEars' Lahn <Point...@nurfuerspam.de> writes:
>> If you would have read the above thoroughly you would have noticed
>> that there is still only one implementation of ECMAScript 4
>> available -- Epimetheus.
>
> I believe JScript.NET is also a (perhaps partial) implementation of
> ECMAScript v4.

Indeed!

<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/jscript7/html/jsgrpecmafeatures.asp>

>> That depends on what you are looking for and where. In ECMAScript 3 and
>> thus JavaScript 1.5 each object has a "constructor" property (inherited
>> from the Object prototype) referring to the constructor used to create the
>> object.
>
> The constructor isn't necessarily inherited. When a user declared
> function is created, its prototype object is also created and assigned
> to the function's "prototype" property. After that, the function is
> assigned to the prototype object's "constructor" property.
> I.e.,
> function foo(){};
> also creates the new object
> foo.prototype
> and makes the assignment
> foo.prototype.constuctor = foo;

There is an "r" missing.

> In this case, the constructor property is not inherited.

Sorry, I fail to see the difference.

>> Since that constructor function (in fact, *every* named function
>> statement) is also the definition for an object prototype, objects created
>> using the Foo() constructor can be referred to as "Foo objects".
>
> Yes, as long as one makes sure not to break the relationship by manually
> manipulating prototypes.

There are always ways to meddle with the defined workings of the object
model. Taking every possible way into account every time one posts a
followup/reply would by far extend the purpose of this newsgroup.

> ---
> function Foo(){ this.x = 42; }
> function Bar(){ this.y = 37; };
> var foo = new Foo();
> var bar = new Bar();
> alert([foo instanceof Foo,
> foo instanceof Bar,
> bar instanceof Foo,
> bar instanceof Bar]); // true,false,true,false
> // swap:
> var fprot = Foo.prototype;
> Foo.prototype = Bar.prototype;
> Bar.prototype = fprot;

The proper way is

Bar.prototype = new Foo;


<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/obj2.html#1008388>


PointedEars

Lasse Reichstein Nielsen

unread,
May 20, 2004, 1:47:12 PM5/20/04
to
Thomas 'PointedEars' Lahn <Point...@nurfuerspam.de> writes:

>
> The proper way is
>
> Bar.prototype = new Foo;

The proper way to what?

> <http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/obj2.html#1008388>

I think this is a bad way of trying (and failing) to emulate class
based inheritance in a non-class based language. Instead of inheriting
from the generic class, you inherit from a single instance (which is
what prototype based inheritance is all about). You lack the call to
the superclass' constructor, and all your instances share the properties
of the prototype.

Example where it fails:
---
function Stack() {
this.stack = [];
}
Stack.prototype.push = function(x){this.stack.push(x);}
Stack.prototype.pop = function(){return this.stack.pop;}

function CountableStack() {}
CountableStack.prototype = new Stack();
CountableStack.prototype.count = function() {return this.stack.length;}
---
This looks plausible, if one reads the Netscape link above. It
fails terribly, since all CountableStack's use the same internal
stack.
---
var s1 = new CountableStack();
var s2 = new CountableStack();
s1.push(42);
s2.push(37);
alert(s1.count());
---

So, IMO, it's *not* a propert way to do anything.

A closer to proper way to make class-like inheritance in Javascript is
(for Bar(x,y,z) extending Foo(x,y)):
---
function Bar(x,y,z) {
Foo.call(this,x,y);
this.z=z;
}
Bar.prototype = clone(Foo.prototype);
---
where clone is
---
function clone(obj) {
function Cloner(){};
Cloner.prototype = obj;
return new Cloner();
}
---
(or *maybe* just use an instance of Foo as prototype, if you know
that it doesn't matter that it has been initialized once).

0 new messages