Getting the class name as a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pavils Jurjans

    Getting the class name as a string

    Hallo,

    Is there some decent way how to get the object class name in a string
    format?

    Currently I use this:

    function getClassName(ob j) {
    if (typeof obj != "object" || obj === null) return false;
    return /(\w+)\(/.exec(obj.const ructor.toString ())[1];
    }

    This approach supports also custom classes.

    But, it seems pretty awkward to do Function.toStri ng to get comperatively
    simple information, a class name. Is there some alternatives?

    Thanks,

    -- Pavils Jurjans
  • Ivo

    #2
    Re: Getting the class name as a string

    "Pavils Jurjans" <pavils@mailbox .riga.lv> wrote in message
    news:95e252b8.0 310140828.6f004 5ca@posting.goo gle.com...[color=blue]
    > Hallo,
    >
    > Is there some decent way how to get the object class name in a string
    > format?[/color]

    What sort of object? A HTML element? How 's this?
    <a class="hello" onclick="alert( this.className) ">Click me</a>
    <a class="hello" onclick="alert( typeof this.className) ">Me too</a>

    [color=blue]
    > Currently I use this:
    >
    > function getClassName(ob j) {
    > if (typeof obj != "object" || obj === null) return false;
    > return /(\w+)\(/.exec(obj.const ructor.toString ())[1];
    > }
    >
    > This approach supports also custom classes.
    >
    > But, it seems pretty awkward to do Function.toStri ng to get comperatively
    > simple information, a class name. Is there some alternatives?
    >
    > Thanks,
    >
    > -- Pavils Jurjans[/color]


    Comment

    • Dom Leonard

      #3
      Re: Getting the class name as a string

      Pavils Jurjans wrote:[color=blue]
      > Hallo,
      >
      > Is there some decent way how to get the object class name in a string
      > format?[/color]

      <rant>

      For native javascript objects (that you construct) label the objects
      with a "className" property string value that you supply or take some
      alternative but otherwise custom approach.

      Seriously, because javascript has no concept of "class name" and
      currently only supports linking objects by means of a prototype chain.
      [color=blue]
      > Currently I use this:
      >
      > function getClassName(ob j) {
      > if (typeof obj != "object" || obj === null) return false;
      > return /(\w+)\(/.exec(obj.const ructor.toString ())[1];
      > }
      >
      > This approach supports also custom classes.[/color]

      Yes, but only for minimalistic prototyping chains.

      When a function object, say F, is created, a hidden property named
      "constructo r", with value F, is generated by the system in a prototype
      object created and used to initialise the ".prototype " property of F.

      So following creation of function object F,

      (F.prototype.co nstructor == F)

      holds true.

      If an object is constructed from function F, the internal prototype
      chain pointer of the constructed object is statically set to the value
      of F.prototype at time of construction, from where a hidden
      "constructo r" property is inherited. So without alteration to the
      prototype chain:

      function F(){};
      var f = new F();
      f.constructor == F; // inherited

      holds true. If the initial value of F.prototype is overwritten, however,
      as in

      function F(){};
      function G(){};
      F.prototype = new G();
      f = new F();

      then f inherits its constructor property value from F.prototype, which
      in turn inherits it from G.prototype, from where its value will be
      returned as G. So now (f.constructor == G) holds true.

      Consider also that a function's prototype property could be set to an
      object expression (showing Object as its constructor) or a preexisting
      object of any kind, including Object.prototyp e for that matter. Agreed
      you won't see examples of this in documentation prepared for class
      oriented programmers if Netscape documentation is any guide to go by.

      Consider also that system supplied prototype objects, created in tandem
      with function objects, have their internal prototype chain (object
      value) set to Object.prototyp e, which lists Object as its constructor.

      Hence ECMAScript does *not* provide a means of enquiring the constructor
      function called to create an object. It does guarantee that the
      constructor property of objects will refer to a function which
      constructed the first and/or second object in a prototype chain starting
      from the Object.protoype end.


      </rant>
      [color=blue]
      >
      > But, it seems pretty awkward to do Function.toStri ng to get comperatively
      > simple information, a class name. Is there some alternatives?
      >[/color]

      ECMAScript supports some operators and object methods (inherited from
      Object.prototyp e) related to prototype chains. These include the "in"
      and "instanceof " binary operators, and hasOwnProperty( ), hasProperty(),
      isPrototypeOf() object methods. Support for any and all of these is
      browser and version dependent, as is appplicability to host or DOM node
      objects. Usefulness of the instanceof operator may assume the near
      universal case that function prototype property values are set for the
      life of a program.


      HTH,

      Dom



      Comment

      • Pavils Jurjans

        #4
        Re: Getting the class name as a string

        Thanks, Dom for your thorough view on this problem

        Rgds,

        -- Pavils

        Comment

        Working...