Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   finding the type of an object? ("typeof" doesn't work) (http://www.velocityreviews.com/forums/t877069-finding-the-type-of-an-object-typeof-doesnt-work.html)

Bennett Haselton 05-01-2004 07:15 PM

finding the type of an object? ("typeof" doesn't work)
 
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/j...pressions.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 05-02-2004 02:47 PM

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


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/


Fox 05-03-2004 03:43 AM

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


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);
>
> And I found this page:
> http://www.mozilla.org/js/language/j...pressions.html
> which claims: "To get the type of an object x, use x.class".


You can't do this yet -- .class is a proposed addition for JS2

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


Not for window or document -- not in IE.

Normally, you would parse the constructor, but window.constructor and
document.constructor are both "undefined" in IE (it's not JavaScript -
it's JScript). In Mozilla, the constructors are returned as "[Window]"
and "[HTMLDocument]" {*not* function Window(...etc... as a "regular"
JavaScript constructor} Furthermore, in IE, window and document are
synonymous (but not exactly equal to each other).

proof:

alert( window == document );

// IE => true; Moz => false


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


for all other javascript objects:

Object.prototype.objectType = function()
{
var obType = String(this.constructor).match(/function\s+(\w+)/);

if(obType) return obType[1];
return "undefined"; // just in case...
}


examples:

function
_myObject()
{
this.anything="";
}

var mo = new -myObject();

alert(mo.objectType()); // type => _myObject

some others (as literals)

alert( (123).objectType()); // => Number

alert( ([1,2,3]).objectType()); // => Array

alert( ("123").objectType()); // => String

alert( (true).objectType()); // => Boolean

and:

function
myFunc()
{
var nada = "";
}

alert( myFunc.objectType() ); // => Function

etc...


if this isn't what you meant... then never mind...

Jim Ley 05-03-2004 12:13 PM

Re: finding the type of an object? ("typeof" doesn't work)
 
On Sun, 02 May 2004 22:43:28 -0500, Fox <fox@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 05-20-2004 07:48 AM

Re: finding the type of an object? ("typeof" doesn't work)
 
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/j...pressions.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 05-20-2004 11:25 AM

Re: finding the type of an object? ("typeof" doesn't work)
 
Thomas 'PointedEars' Lahn <PointedEars@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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Thomas 'PointedEars' Lahn 05-20-2004 03:33 PM

Re: finding the type of an object? ("typeof" doesn't work)
 
Lasse Reichstein Nielsen wrote:

> Thomas 'PointedEars' Lahn <PointedEars@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 05-20-2004 05:47 PM

Re: finding the type of an object? ("typeof" doesn't work)
 
Thomas 'PointedEars' Lahn <PointedEars@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).
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


All times are GMT. The time now is 01:30 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.