#727 ✓wont_fix


Reported by Semmu | June 28th, 2009 @ 02:49 PM


I'm creating a JS Lib based on Prototype, but I am beginner, and I need to get the name of an Object (like Array).
I know there's something called "instanceof" but it's boolean, so I can't use it for get an object's name.
But I found a function, and with this func. I can get it.
I think it's very useful, so I want you to add this method into the new version of prototype! :)

Object.prototype.get_objectname = function()

var sCon = this.constructor.toString();
if (sCon)
return sCon.slice(sCon.indexOf(' ') + 1, sCon.indexOf('('));
return null;


You can rename it of course, if you want :)

Semmu from Hungary

P.S.: sorry for my bad english :S

Comments and changes to this ticket

  • Tobie Langel

    Tobie Langel June 29th, 2009 @ 03:17 AM

    • State changed from “new” to “wont_fix”

    Thank you for the suggestion. Function decompilation is not supported on all platforms, unspecified and generally untrustworthy. It's therefore better avoided.

  • Kit Goncharov

    Kit Goncharov June 29th, 2009 @ 05:50 AM

    I might be missing something, but doesn't Prototype already include this functionality in its getClass method? (it's currently stored in a closure, but could probably be exposed as Object.getClass if needed). If all you're looking for is an object's [[Class]] name (Array, Object, Date, RegExp, etc.), the solution below should work:

    //The actual implementation in Prototype 1.6.1 RC3
    function getClass(object){
        return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
    getClass([]) => 'Array'
    getClass({}) => 'Object'
    getClass(function(){return this;}) => 'Function'

    This still isn't a foolproof solution, as some browsers will revert to using Object as a general [[Class]] name (Error, for example, is unreliable; some browsers just return Object as its [[Class]] name). This is also not the best way to detect host objects (window), but it's definitely more reliable than function decompilation, and will definitely work for the core classes (such as String or Array).

    Also, just a side note: be careful when extending Object.prototype. It's a good idea in theory (and makes calling methods across objects so much easier), but will break enumeration with for...in loops. A better approach might be to either add the method to the Object namespace (i.e., Object.getClass) or as an instance method to the Hash class (Hash.addMethods({getClass: ...})).

  • Tobie Langel

    Tobie Langel June 29th, 2009 @ 01:47 PM

    @Kit S. Goncharov: As per ES3 specs, your suggestion unfortunately doesn't work for custom objects, nor, as you mentioned, for host objects.

    However, exporting the getClassName API might be something worth doing in the future.

  • Juriy Zaytsev

    Juriy Zaytsev June 29th, 2009 @ 06:39 PM

    @Kit S. Goncharov If [[Class]] of Error is reported as "Object", than that makes implementation non-compliant :) See in ES3. Is it Safari 2.x by any chance?

    @Tobie What's a custom object?

  • Kit Goncharov

    Kit Goncharov June 29th, 2009 @ 07:28 PM

    @Juriy: Yep, it's Safari 2.x all right. I continue to be amazed at just how non-compliant this browser is...time for me to go read the ES3 specs and be horrified! :)

    @Tobie: I'm not really sure what a custom object is, either. Am I grasping the general idea with the example below, or are you referring to something completely different?

    var myClass = Class.create({initialize: ...});
    getName(myClass) => 'Class'
  • Tobie Langel

    Tobie Langel June 30th, 2009 @ 12:45 AM

    Custom objects is the term generally used in JS literature to describe objects defined by the developer himself.

    In which case a getClassName function is pretty useless as it correctly returns "Object" as per specs.

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:01 AM

    • Tag changed from function, get, name, new, object to function, section:lang

    [not-tagged:"object" tagged:"section:lang" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:07 AM

    • Tag changed from function, section:lang to section:lang

    [not-tagged:"function" bulk edit command]

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

People watching this ticket