Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Get Function name when prototyping

Reply
Thread Tools

Get Function name when prototyping

 
 
Laser Lips
Guest
Posts: n/a
 
      07-03-2009
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
 
Reply With Quote
 
 
 
 
Laser Lips
Guest
Posts: n/a
 
      07-03-2009
Just to be clear, I'm asking how can I get the function name of a
prototyped function?
Thank you.
Graham
 
Reply With Quote
 
 
 
 
Jorge
Guest
Posts: n/a
 
      07-03-2009
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.
 
Reply With Quote
 
John G Harris
Guest
Posts: n/a
 
      07-03-2009
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
 
Reply With Quote
 
Garrett Smith
Guest
Posts: n/a
 
      07-04-2009
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/
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      07-04-2009
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>
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      07-04-2009
On Jul 4, 8:39*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> (...)
> It does _not_ fail in JScript, but it works like an out-of-place (global?)
> function declaration;
> (...)


No, not global.

--
Jorge.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      07-05-2009
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$>
 
Reply With Quote
 
JR
Guest
Posts: n/a
 
      07-05-2009
On Jul 5, 6:41*am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
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$8300d...@news.demon.co.uk>


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

--
JR
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      07-05-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function prototyping at runtime Philliam Auriemma C++ 1 04-16-2010 08:40 PM
function prototyping? Burton Samograd Python 12 04-14-2006 07:01 AM
Announcing new scripting/prototyping language Dave Allison C++ 37 03-04-2004 05:45 PM
Re: No call for Ada (was Re: Announcing new scripting/prototyping language) Ludovic Brenta C++ 86 02-19-2004 09:19 AM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57