Get Function name when prototyping

Discussion in 'Javascript' started by Laser Lips, Jul 3, 2009.

  1. Laser Lips

    Laser Lips Guest

    Hi All,
    When I wish to dynamically get the name of a function I can use
    arguments.callee.toString() to work out the function name.

    But when prototyping functions like in the following example, the
    function name is emitted.

    <script tyle='text/javascript'>
    function hello()
    {
    alert(arguments.callee.toString());
    }
    hello();

    function DCS(){}
    DCS.prototype.hello=function()
    {
    alert(arguments.callee.toString());
    }
    var DCS=new DCS();
    DCS.hello();
    </script>


    I guess this has been asked before but I can't find anything.
    Thank you,
    Graham Vincent
     
    Laser Lips, Jul 3, 2009
    #1
    1. Advertising

  2. Laser Lips

    Laser Lips Guest

    Just to be clear, I'm asking how can I get the function name of a
    prototyped function?
    Thank you.
    Graham
     
    Laser Lips, Jul 3, 2009
    #2
    1. Advertising

  3. Laser Lips

    Jorge Guest

    Laser Lips wrote:
    > Just to be clear, I'm asking how can I get the function name of a
    > prototyped function?
    > Thank you.
    > Graham


    javascript:alert((function f () {}).name);

    --
    Jorge.
     
    Jorge, Jul 3, 2009
    #3
  4. On Fri, 3 Jul 2009 at 02:03:33, in comp.lang.javascript, Laser Lips
    wrote:
    >Hi All,
    >When I wish to dynamically get the name of a function I can use
    >arguments.callee.toString() to work out the function name.
    >
    >But when prototyping functions like in the following example, the
    >function name is emitted.
    >
    ><script tyle='text/javascript'>
    >function hello()
    >{
    > alert(arguments.callee.toString());
    >}
    >hello();
    >
    >function DCS(){}
    >DCS.prototype.hello=function()

    ^^^ Look! No name!
    >{
    > alert(arguments.callee.toString());
    >}
    >var DCS=new DCS();
    >DCS.hello();
    ></script>


    John
    --
    John Harris
     
    John G Harris, Jul 3, 2009
    #4
  5. Laser Lips wrote:
    > Hi All,
    > When I wish to dynamically get the name of a function I can use
    > arguments.callee.toString() to work out the function name.
    >
    > But when prototyping functions like in the following example, the
    > function name is emitted.
    >


    There is a big difference between "omitted" and "emitted".

    A FunctionExpression may have an optional identifier.

    However in some engines, it fails. The engine in Safari <= 2 and JScript
    have problems. Otherwise, we could write:-

    function DCS() {}
    DCS.prototype.hello = function hello() {
    alert("hello");
    };

    - and it would work consistently in more than a few browsers. Safari 2
    is dying out, but unfortunately, we have a lot of IE users to contend with.

    > <script tyle='text/javascript'>
    > function hello()
    > {
    > alert(arguments.callee.toString());
    > }
    > hello();
    >
    > function DCS(){}
    > DCS.prototype.hello=function()
    > {
    > alert(arguments.callee.toString());
    > }
    > var DCS=new DCS();
    > DCS.hello();
    > </script>
    >


    If you want to use an identifier, use a function declaration in the same
    scope. Don't use global scope, as that will create an irrelevant method
    of the global object. Instead, try:-

    var pkg = {};
    (function() {
    pkg.DCS = DCS;

    function DCS() {

    }

    DCS.prototype = {
    hello : hello,
    type : 1
    };

    /** Instance Method */
    function hello(){
    alert("mmm " + this.type);
    }
    })();

    new pkg.DCS().hello();

    >
    > I guess this has been asked before but I can't find anything.


    The topic has been discussed a lot. Search for FunctionExpression and
    FunctionDeclaration and FunctionStatement in the archives.

    See also:
    http://yura.thinkweb2.com/named-function-expressions/

    > Thank you,
    > Graham Vincent


    Garrett

    --
    comp.lang.javascript FAQ: http://jibbering.com/faq/
     
    Garrett Smith, Jul 4, 2009
    #5
  6. Garrett Smith wrote:
    > A FunctionExpression may have an optional identifier.
    >
    > However in some engines, it fails. The engine in Safari <= 2 and JScript
    > have problems. Otherwise, we could write:-
    >
    > function DCS() {}
    > DCS.prototype.hello = function hello() {
    > alert("hello");
    > };
    >
    > - and it would work consistently in more than a few browsers. Safari 2
    > is dying out, but unfortunately, we have a lot of IE users to contend with.


    It does _not_ fail in JScript, but it works like an out-of-place (global?)
    function declaration; after this statement, `hello' is available as a
    reference to the function when it shouldn't be.

    First time that I heard it failed in Safari 2.

    > [...]
    > The topic has been discussed a lot. Search for FunctionExpression and
    > FunctionDeclaration and FunctionStatement in the archives.


    And how named FunctionExpressions are really working in JScript has also
    been discussed several times. You seem to have missed it.

    > See also:
    > http://yura.thinkweb2.com/named-function-expressions/


    I haven't read that (maybe later), but if what you said above is any
    indication, you should dump it.


    PointedEars
    --
    Use any version of Microsoft Frontpage to create your site.
    (This won't prevent people from viewing your source, but no one
    will want to steal it.)
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
     
    Thomas 'PointedEars' Lahn, Jul 4, 2009
    #6
  7. Laser Lips

    Jorge Guest

    On Jul 4, 8:39 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > (...)
    > It does _not_ fail in JScript, but it works like an out-of-place (global?)
    > function declaration;
    > (...)


    No, not global.

    --
    Jorge.
     
    Jorge, Jul 4, 2009
    #7
  8. kangax wrote:
    > Thomas 'PointedEars' Lahn wrote:
    >> Garrett Smith wrote:
    >>> A FunctionExpression may have an optional identifier.
    >>>
    >>> However in some engines, it fails. The engine in Safari <= 2 and JScript
    >>> have problems. Otherwise, we could write:-
    >>>
    >>> function DCS() {}
    >>> DCS.prototype.hello = function hello() {
    >>> alert("hello");
    >>> };
    >>>
    >>> - and it would work consistently in more than a few browsers. Safari 2
    >>> is dying out, but unfortunately, we have a lot of IE users to contend with.

    >> It does _not_ fail in JScript, but it works like an out-of-place (global?)

    >
    > Identifier leaks into an enclosing scope, not global one. See example #1
    > <http://yura.thinkweb2.com/named-function-expressions/#jscript-bugs>


    Thanks.

    >> function declaration; after this statement, `hello' is available as a
    >> reference to the function when it shouldn't be.
    >>
    >> First time that I heard it failed in Safari 2.

    >
    > Just read an article. I described Safari 2 situation in details there.
    >
    > <http://yura.thinkweb2.com/named-function-expressions/#safari-bug>
    >
    > And no, it doesn't *always* "fail" in Safari; at least not in this
    > particular example that Garrett gave (`DCS.prototype.hello = function
    > hello(){ ... }`).


    That's in fact the old (3.5.1) KJS bug I mentioned some time ago (also
    documented in line 281 of current object.js 0.1.3.2006100822). Konqueror
    uses KJS as script engine, as does WebKit. This bug has been fixed, see
    <http://bugs.kde.org/show_bug.cgi?id=123529>.


    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
     
    Thomas 'PointedEars' Lahn, Jul 5, 2009
    #8
  9. Laser Lips

    JR Guest

    On Jul 5, 6:41 am, Thomas 'PointedEars' Lahn <>
    wrote:
    > kangax wrote:
    > > Thomas 'PointedEars' Lahn wrote:
    > >> Garrett Smith wrote:
    > >>> A FunctionExpression may have an optional identifier.

    >
    > >>> However in some engines, it fails. The engine in Safari <= 2 and JScript
    > >>> have problems. Otherwise, we could write:-

    >
    > >>> function DCS() {}
    > >>> DCS.prototype.hello = function hello() {
    > >>>    alert("hello");
    > >>> };

    >
    > >>> - and it would work consistently in more than a few browsers. Safari 2
    > >>> is dying out, but unfortunately, we have a lot of IE users to contendwith.
    > >> It does _not_ fail in JScript, but it works like an out-of-place (global?)

    >
    > > Identifier leaks into an enclosing scope, not global one. See example #1
    > > <http://yura.thinkweb2.com/named-function-expressions/#jscript-bugs>

    >
    > Thanks.
    >
    > >> function declaration; after this statement, `hello' is available as a
    > >> reference to the function when it shouldn't be.

    >
    > >> First time that I heard it failed in Safari 2.

    >
    > > Just read an article. I described Safari 2 situation in details there.

    >
    > > <http://yura.thinkweb2.com/named-function-expressions/#safari-bug>

    >
    > > And no, it doesn't *always* "fail" in Safari; at least not in this
    > > particular example that Garrett gave (`DCS.prototype.hello = function
    > > hello(){ ... }`).

    >
    > That's in fact the old (3.5.1) KJS bug I mentioned some time ago (also
    > documented in line 281 of current object.js 0.1.3.2006100822).  Konqueror
    > uses KJS as script engine, as does WebKit.  This bug has been fixed, see
    > <http://bugs.kde.org/show_bug.cgi?id=123529>.
    >
    > PointedEars
    > --
    > Prototype.js was written by people who don't know javascript for people
    > who don't know javascript. People who don't know javascript are not
    > the best source of advice on designing systems that use javascript.
    >   -- Richard Cornford, cljs, <f806at$ail$1$>


    Hey, 'Kangax' is a Prototype.js core developer and he does know
    Javascript a lot.

    --
    JR
     
    JR, Jul 5, 2009
    #9
  10. Laser Lips

    Jorge Guest

    Thomas 'PointedEars' Lahn wrote:
    > (...)
    > Konqueror uses KJS as script engine, as does WebKit.
    > (...)


    LOL, that's not true. KHTML was forked into WebCore and KJS into
    JavaScriptCore many years ago. Both evolved independently ever since...
    until a day came in 2007 -after years of split- when KDE's team came
    back wanting to undo the forking in order to benefit of Apple's now much
    superior rendering engine and JS VM. If this has ever finally
    materialised (which I don't think so), all that you could properly say
    is that Konqueror now uses Apple's (webkit's) rendering engine and/or JS
    VM, but of course not the other way around.

    Because after so many years of evolution, any similarities that may
    exist between KDE's KJS and Apple's NITRO are pure coincidence, ISTM.

    --
    Jorge
     
    Jorge, Jul 5, 2009
    #10
  11. Laser Lips

    David Mark Guest

    On Jul 5, 5:19 pm, JR <> wrote:
    > On Jul 5, 6:41 am, Thomas 'PointedEars' Lahn <>
    > wrote:
    >
    >
    >
    > > kangax wrote:
    > > > Thomas 'PointedEars' Lahn wrote:
    > > >> Garrett Smith wrote:
    > > >>> A FunctionExpression may have an optional identifier.

    >
    > > >>> However in some engines, it fails. The engine in Safari <= 2 and JScript
    > > >>> have problems. Otherwise, we could write:-

    >
    > > >>> function DCS() {}
    > > >>> DCS.prototype.hello = function hello() {
    > > >>>    alert("hello");
    > > >>> };

    >
    > > >>> - and it would work consistently in more than a few browsers. Safari 2
    > > >>> is dying out, but unfortunately, we have a lot of IE users to contend with.
    > > >> It does _not_ fail in JScript, but it works like an out-of-place (global?)

    >
    > > > Identifier leaks into an enclosing scope, not global one. See example#1
    > > > <http://yura.thinkweb2.com/named-function-expressions/#jscript-bugs>

    >
    > > Thanks.

    >
    > > >> function declaration; after this statement, `hello' is available as a
    > > >> reference to the function when it shouldn't be.

    >
    > > >> First time that I heard it failed in Safari 2.

    >
    > > > Just read an article. I described Safari 2 situation in details there..

    >
    > > > <http://yura.thinkweb2.com/named-function-expressions/#safari-bug>

    >
    > > > And no, it doesn't *always* "fail" in Safari; at least not in this
    > > > particular example that Garrett gave (`DCS.prototype.hello = function
    > > > hello(){ ... }`).

    >
    > > That's in fact the old (3.5.1) KJS bug I mentioned some time ago (also
    > > documented in line 281 of current object.js 0.1.3.2006100822).  Konqueror
    > > uses KJS as script engine, as does WebKit.  This bug has been fixed, see
    > > <http://bugs.kde.org/show_bug.cgi?id=123529>.

    >
    > > PointedEars
    > > --
    > > Prototype.js was written by people who don't know javascript for people
    > > who don't know javascript. People who don't know javascript are not
    > > the best source of advice on designing systems that use javascript.
    > >   -- Richard Cornford, cljs, <f806at$ail$1$>

    >
    > Hey, 'Kangax' is a Prototype.js core developer and he does know
    > Javascript a lot.


    And yet, Prototype is still a lost cause. Go figure.
     
    David Mark, Jul 7, 2009
    #11
  12. kangax wrote:
    > Thomas 'PointedEars' Lahn wrote:
    >> kangax wrote:

    > [...]
    >>> <http://yura.thinkweb2.com/named-function-expressions/#safari-bug>
    >>>
    >>> And no, it doesn't *always* "fail" in Safari; at least not in this
    >>> particular example that Garrett gave (`DCS.prototype.hello = function
    >>> hello(){ ... }`).

    >> That's in fact the old (3.5.1) KJS bug I mentioned some time ago (also
    >> documented in line 281 of current object.js 0.1.3.2006100822). Konqueror
    >> uses KJS as script engine, as does WebKit. This bug has been fixed, see
    >> <http://bugs.kde.org/show_bug.cgi?id=123529>.

    >
    > Actually, Konqueror bug looks like a subset of Safari one's, but I don't
    > think they are related. Safari fails with named fun. expr. in object
    > literals for a different reason. In Safari, named fun. expr. only
    > "works" when it is an /AssignmentExpression/, so something like -
    >
    > `var o = { foo: function bar(){} }`
    >
    > - will fail, while something like -
    >
    > `var f; var o = { foo: (f = function bar(){}) }`
    >
    > - will work.


    I cannot test that particular case in KJS 3.5.1 because I don't have the old
    Konqueror anymore. However, the connection between KJS and WebKit is most
    obvious. RTSL.


    PointedEars
    --
    Use any version of Microsoft Frontpage to create your site.
    (This won't prevent people from viewing your source, but no one
    will want to steal it.)
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
     
    Thomas 'PointedEars' Lahn, Jul 7, 2009
    #12
  13. David Mark wrote:
    > On Jul 5, 5:19 pm, JR <> wrote:
    >> On Jul 5, 6:41 am, Thomas 'PointedEars' Lahn <>
    >> wrote:
    >>> --
    >>> Prototype.js was written by people who don't know javascript for people
    >>> who don't know javascript. People who don't know javascript are not
    >>> the best source of advice on designing systems that use javascript.
    >>> -- Richard Cornford, cljs, <f806at$ail$1$>

    >> Hey, 'Kangax' is a Prototype.js core developer and he does know
    >> Javascript a lot.

    >
    > And yet, Prototype is still a lost cause. Go figure.


    JFTR: It was a random signature.


    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
     
    Thomas 'PointedEars' Lahn, Jul 7, 2009
    #13
    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. Dave Allison
    Replies:
    37
    Views:
    820
    Warren W. Gay VE3WWG
    Mar 4, 2004
  2. Ludovic Brenta
    Replies:
    86
    Views:
    1,416
    Jan C. =?iso-8859-1?Q?Vorbr=FCggen?=
    Feb 19, 2004
  3. rs
    Replies:
    13
    Views:
    519
    Mark Gordon
    Dec 1, 2003
  4. Burton Samograd

    function prototyping?

    Burton Samograd, Apr 13, 2006, in forum: Python
    Replies:
    12
    Views:
    7,043
    Dennis Lee Bieber
    Apr 14, 2006
  5. Philliam Auriemma

    Function prototyping at runtime

    Philliam Auriemma, Apr 16, 2010, in forum: C++
    Replies:
    1
    Views:
    271
    Öö Tiib
    Apr 16, 2010
Loading...

Share This Page