Results 1 to 5 of 5

Thread: How well would this work?

  1. #1
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default How well would this work?

    I made a simple function:
    Code:
    const constructorof = function (data) {
    	return data.constructor.toString ().split ("(") [0].split (" ") [1];
    }
    How well would it work?
    Also, it works for custom prototypes if the custom constructor was made with a
    Code:
    function thing () {}
    but
    Code:
    var thing = function () {}
    doesn't work, is there any way I can make it?

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    I believe const is not supported by IE. Use var instead.

    The constructor of
    Code:
    var thing = function () {}
    is Function
    Trinithis

  3. #3
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    I also doubt that there is a real use for it anyway. If you hadn't known, you can store the constructor you get through the constructor property into a variable and use it to create objects.

    Code:
    var s = "foo";
    var c = s.constructor;
    alert(c(false)); //alerts "false"
    alert(typeof c(false)); //alerts "string"
    Trinithis

  4. #4
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    It was just a random 5 second idea, but constructor is a function, and it simply getting a string for you.
    Last edited by ???; 08-12-2007 at 04:26 AM.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    How well would it work?
    Nasty hack, but possibly necessary. The best way to do it would be:
    Code:
    function constructorof(o) {
      return o.constructor.name;
    }
    ... however, some browsers don't support this (IE, I think, included), so your idea's as good as any. It won't, however, work with anonymous functions, since they don't have a name to return (by definition).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •