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

Discussion in 'Javascript' started by Bennett Haselton, May 1, 2004.

  1. 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
    Bennett Haselton, May 1, 2004
    #1
    1. Advertising

  2. 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/
    Martin Honnen, May 2, 2004
    #2
    1. Advertising

  3. Bennett Haselton

    Fox Guest

    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/js20-2002-04/core/expressions.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...
    Fox, May 3, 2004
    #3
  4. Bennett Haselton

    Jim Ley Guest

    On Sun, 02 May 2004 22:43:28 -0500, Fox <> 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/
    Jim Ley, May 3, 2004
    #4
  5. 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
    Thomas 'PointedEars' Lahn, May 20, 2004
    #5
  6. Thomas 'PointedEars' Lahn <> 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, May 20, 2004
    #6
  7. Lasse Reichstein Nielsen wrote:

    > Thomas 'PointedEars' Lahn <> 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
    Thomas 'PointedEars' Lahn, May 20, 2004
    #7
  8. Thomas 'PointedEars' Lahn <> 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, May 20, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leon

    Using TypeOf?

    Leon, Nov 1, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    2,152
    Henri
    Nov 1, 2004
  2. M. Steinbach
    Replies:
    1
    Views:
    632
    tom_usenet
    Feb 20, 2004
  3. M. Steinbach
    Replies:
    0
    Views:
    345
    M. Steinbach
    Feb 20, 2004
  4. Barry Kelly
    Replies:
    5
    Views:
    582
    Barry Kelly
    Jun 21, 2006
  5. James Marshall

    Determining object type, finer-grained than typeof

    James Marshall, Nov 6, 2003, in forum: Javascript
    Replies:
    3
    Views:
    150
    Jim Ley
    Nov 6, 2003
Loading...

Share This Page