By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

Re: anonymous functions; naming of and firebug

P: n/a
On Feb 17, 4:58 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
mk83...@yahoo.com wrote:
anchors[ndx].onclick = function aname() {
Firebug still lists an anonymous function. Is there a way to assign a
name?

You have assigned a name with the above, but Firebug never shows the name.
That should not come as a surprise because a) the name is only locally
available (except in JScript) and b) functions don't have a certain name in
the greater context.
In Spidermonkey Functions absolutely do have a name:-

var x = function y() {
console.log(arguments.callee.name);
};

x();

Result: y

As for the problem of inspecting the LI, logging it should have the
effect:

console.log( li );
Garrett

[snip]
PointedEars
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
dhtml wrote:
On Feb 17, 4:58 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>mk83...@yahoo.com wrote:
>> anchors[ndx].onclick = function aname() {
Firebug still lists an anonymous function. Is there a way to assign a
name?
You have assigned a name with the above, but Firebug never shows the name.
That should not come as a surprise because a) the name is only locally
available (except in JScript) and b) functions don't have a certain name in
the greater context.

In Spidermonkey Functions absolutely do have a name:-
You miss the point. I have said that Functions do _not_ have a *certain*
name in the greater context. That really is basic knowledge:
var x = function y() {
console.log(arguments.callee.name);
};

x();

Result: y
See? Would you really expect the function to be displayed as `y' despite
the property that is referring to it has the name `x'?

And what about

o.z = function y() {
console.log(arguments.callee.name);
returns false;
};

Would you then expect Firebug displaying `y' as well despite `o.z' refers to
a completely different Function object? Most certainly not.
As for the problem of inspecting the LI, logging it should have the
effect:

console.log( li );
That will of course display only the start tag of the element, a string
representation of the element node and its attribute nodes. Since `onclick'
is an attribute in the markup but not accessed as such here (else you would
assign a string value and not a Function object reference), there is no
change in its representation. (If one used setAttribute(), the change would
be visible in Firebug, but the script would not be likely to work everywhere.)
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, <f8*******************@news.demon.co.uk>
Jun 27 '08 #2

P: n/a
On Apr 14, 12:24 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
dhtml wrote:
On Feb 17, 4:58 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
mk83...@yahoo.com wrote:
anchors[ndx].onclick = function aname() {
Firebug still lists an anonymous function. Is there a way to assign a
name?
You have assigned a name with the above, but Firebug never shows the name.
That should not come as a surprise because a) the name is only locally
available (except in JScript) and b) functions don't have a certain name in
the greater context.
In Spidermonkey Functions absolutely do have a name:-

You miss the point. I have said that Functions do _not_ have a *certain*
name in the greater context. That really is basic knowledge:
var x = function y() {
console.log(arguments.callee.name);
};
x();
Result: y

See? Would you really expect the function to be displayed as `y' despite
the property that is referring to it has the name `x'?
It doesn't have a name x. x is a reference to a function. That
function has a name property, with the value y. The name property
reflects the function's identifier.

It is entirely possible to have more than one variable point to the
same function. For example:

var a = [], z;
z = a[0] = function y(){};

I would not expect the function's name to be a[0].

>
And what about

o.z = function y() {
console.log(arguments.callee.name);
returns false;
};

Would you then expect Firebug displaying `y' as well despite `o.z' refers to
a completely different Function object? Most certainly not.
The function that o.z points to has a name. The name is y. It seems to
me to be a natural expectation that function.name would return the
identifier of the function.

It also is a good idea to give a meaningful and unique name to the
function. Of course, the problem with adding an identifier is that old
JScript bug. There's also been some bugs in versions of Safari as
recent as 2.
>
As for the problem of inspecting the LI, logging it should have the
effect:
console.log( li );

That will of course display only the start tag of the element, a string
representation of the element node and its attribute nodes.
That is what clicking on it is for.

Since `onclick'
is an attribute in the markup but not accessed as such here (else you would
assign a string value and not a Function object reference), there is no
change in its representation. (If one used setAttribute(), the change would
be visible in Firebug, but the script would not be likely to work everywhere.)


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>
Jun 27 '08 #3

P: n/a
dhtml wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
>dhtml wrote:
>>On Feb 17, 4:58 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
mk83...@yahoo.com wrote:
anchors[ndx].onclick = function aname() {
Firebug still lists an anonymous function. Is there a way to assign a
name?
You have assigned a name with the above, but Firebug never shows the name.
That should not come as a surprise because a) the name is only locally
available (except in JScript) and b) functions don't have a certain name in
the greater context.
In Spidermonkey Functions absolutely do have a name:-
You miss the point. I have said that Functions do _not_ have a *certain*
name in the greater context. That really is basic knowledge:
>>var x = function y() {
console.log(arguments.callee.name);
};
x();
Result: y
See? Would you really expect the function to be displayed as `y' despite
the property that is referring to it has the name `x'?

It doesn't have a name x. x is a reference to a function.
*yawn*

To a Function object. I know. You miss the point again.
That function has a name property, with the value y.
In one implementation.
The name property reflects the function's identifier.
As an ECMAScript extension, it stores the name of a locally available property:

,-[ECMAScript Ed. 3 Final, section 13]
|
| NOTE The Identifier in a FunctionExpression can be referenced from inside
| the FunctionExpression's FunctionBody to allow the function to call itself
| recursively. However, unlike in a FunctionDeclaration, the Identifier in
| a FunctionExpression cannot be referenced from and does not affect the
| scope enclosing the FunctionExpression.
It is entirely possible to have more than one variable point to the
same function.
I know. That is exactly one of my points. You cannot expect Firebug to
display the name there.
>And what about

o.z = function y() {
console.log(arguments.callee.name);
returns false;
};

Would you then expect Firebug displaying `y' as well despite `o.z' refers to
a completely different Function object? Most certainly not.

The function that o.z points to has a name. The name is y. It seems to
me to be a natural expectation that function.name would return the
identifier of the function.
It would be nonsense for Firebug's DOM inspector to display the same name
for different Function objects just because they happen to have a locally
available property with the same name. For example, it would lead to all
kinds of confusion if an object had

o.onclick = function x() { return true; };
o.onmouseup = function x() { return false; };

and Firebug's DOM inspector were to display

onclick x()
onmouseup x()

instead of what it currently displays:

onclick function()
onmouseup function()

The name property of a Function object, if it has one, is completely
irrelevant outside its local execution context.
It also is a good idea to give a meaningful and unique name to the
function.
It is in a function declaration, not in a function expression.
Of course, the problem with adding an identifier is that old
JScript bug.
Therefore, it is not a good idea.
There's also been some bugs in versions of Safari as recent as 2.
Therefore, it is an even worse idea.
>>As for the problem of inspecting the LI, logging it should have the
effect:
console.log( li );
That will of course display only the start tag of the element, a string
representation of the element node and its attribute nodes.

That is what clicking on it is for.
Have you ever done that for an object that has an event listener added?
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, <f8*******************@news.demon.co.uk>
Jun 27 '08 #4

P: n/a
dhtml <dh**********@gmail.comwrites:
> o.z = function y() {
....
The function that o.z points to has a name. The name is y. It seems to
me to be a natural expectation that function.name would return the
identifier of the function.
That would have been a reasonable design. Alas, it isn't so. There
is no direct way to access that name from outside its scope. Its
scope is the body of the function.

On the other hand, it's also completely useless outside of its scope,
for anything but documentation purposes, so it's no big loss in
practice.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 27 '08 #5

P: n/a
On Apr 16, 3:07*am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
dhtml wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
dhtml wrote:
On Feb 17, 4:58 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
mk83...@yahoo.com wrote:
* * *anchors[ndx].onclick = function *aname() {

The spec does not contain a - name - property for a function object.
It's there in Spidermonkey.
>
It would be nonsense for Firebug's DOM inspector to display the same name
for different Function objects just because they happen to have a locally
available property with the same name. *For example, it would lead to all
kinds of confusion if an object had

* o.onclick = function x() { return true; };
* o.onmouseup = function x() { return false; };

and Firebug's DOM inspector were to display

* onclick * *x()
* onmouseup *x()

instead of what it currently displays:

* onclick * *function()
* onmouseup *function()

The name property of a Function object, if it has one, is completely
irrelevant outside its local execution context.
It works in Spidermonkey. When I try this in Firebug, I can see that
the name - y - will get printed out. It's possibly a different version
of Firebug.

document.body.onclick = function y(){};
document.body

This displays in Firebug:
"<body>"

When I click on that <body>, then choose the right-side DOM tab, I can
see
onclick y()

For me, that makes it easier to debug.

It's possible that I could have two functions with y, but I would
probably not do that. If I did, I'd still ahve it narrowed down to two
functions in the search result.

>
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>- Hide quoted text -

- Show quoted text -
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.