Get Function name when prototyping

L

Laser Lips

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
 
L

Laser Lips

Just to be clear, I'm asking how can I get the function name of a
prototyped function?
Thank you.
Graham
 
J

Jorge

Laser said:
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);
 
J

John G Harris

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
 
G

Garrett Smith

Laser said:
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
 
T

Thomas 'PointedEars' Lahn

Garrett said:
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.

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


PointedEars
 
T

Thomas 'PointedEars' Lahn

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


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
 
J

JR

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, <[email protected]>

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

Jorge

Thomas said:
(...)
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.
 
T

Thomas 'PointedEars' Lahn

kangax said:
Thomas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top