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/