SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Location
    Leeds, UK
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting given name of a function in IE

    This seems simple, but I'm getting nowhere. The below works in Mozilla;

    Code:
    function someFunction() {
        // Doesn't matter what the function does
    }
    
    alert(someFunction.name)
    Mozilla: 'someFunction'
    IE6/7: 'undefined'

    (On it's own this looks pointless, but I'm using it as part of something else where the name of the function forms part of a named index in an array for quicker referencing later than using a for/in loop).

  2. #2
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function getFuncName( fn ) 
    {
    	str = fn.toString();
    	str = str.substr('function '.length);
    	str = str.substr(0, str.indexOf('('));
    	return str;
    }
    alert( getFuncName( funcRef ) );

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Location
    Leeds, UK
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect, thanks a lot!

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Location
    Leeds, UK
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    (I've shortened this if interested)

    Code:
    function getFuncName( fn ) 
    {
        return fn.toString().match(/function (\w+)/)[1];
    }
    alert( getFuncName( funcRef ) );

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mozilla development center

    Core JavaScript 1.5 Reference:Global Objects:Function

    Properties
    arguments Deprecated*: An array corresponding to the arguments passed to a function. This is deprecated as property of Function, use the arguments object available within the function instead.
    arity Deprecated*: Specifies the number of arguments expected by the function. Use the length property instead.
    caller: Specifies the function that invoked the currently executing function (non-standard).
    constructor: Specifies the function that creates an object's prototype.
    length: Specifies the number of arguments expected by the function.
    name: The name of the function (not part of the standard).
    prototype: Allows the addition of properties to function objects (both those constructed using Function and those that were declared using a function declaration or a function expression).

    http://developer.mozilla.org/en/docs...ion#Properties
    ...


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
  •