5

This is an edge case and probably bad practice, but it made me curious about some js internals. Can anyone explain why chrome dev tools tells me that I have created a function named a.a.b.b here?

enter image description here

Note that this does not happen unless you are assigning to a property. Otherwise both a and b appear to refer to a function object named 'b':

enter image description here

By the way, I originally encountered this here when trying to answer my own question about dat.gui.js .

3
  • 3
    You're being misled by the odd ways in which browser consoles work. (The language doesn't do what you think it's doing. Anonymous functions don't really have names.) Commented May 31, 2015 at 3:57
  • 3
    To @Pointy's point, if you try to call a.a.b.b() you'll get a TypeError. Commented May 31, 2015 at 3:58
  • It has nothing to do with JS internals. It's just a DevTools enhancement, for debugging convenience I guess. The real assigned value is still anonymous function - a.a.name gives you empty string. Commented May 31, 2015 at 4:02

1 Answer 1

3

This has nothing to do with the language spec.
It's a DevTools enhancement for debugging convenience, which is ported recently in Chrome.

Remember what we used to do?

function F() {}
// notice it's a NAMED function expression
F.prototype.asdf = function _asdf() { debugger; }; 

var f = new F();
f.asdf();

Then in breakpoint debugging, we can find the method by its name _asdf from function call stack. Otherwise it's the pain in the ass to do that from a list of (anonymous function).

enter image description here

In latest Chrome, when you assign an anonymous function as an object property, an alias will be attached to it.

var a = {}, b = {};
a.a = b.b = function() { debugger; };
a.b = b.a = function _abba() { debugger; };

Remember, it's just a DevTools enhancement, the method remains anonymous:

a.a.name; // ""
a.b.name; // "_abba"

But it's very helpful in breakpoint debugging:

a.a();
a.b();

enter image description here


EDIT:

I'm not very sure why the alias is generated as a.a.b.b, it looks very easy but kind of... stupid. However, in practice we seldom do a.a = b.b = func... thing (lucky). Instead, we define a method in one place, and do inheritence when necessary, rather than copy reference directly.

So in a good programming practice, the alias should and would exactly reflect where you define the method. For example, alias Dog.bark in breakpoint clearly maps to Dog.prototype.bark in source code, even if it's called on a Puppy instance, and we don't have to do old school named function expression.

function Dog() {}
Dog.prototype.bark = function() { alert("Woof!") }; // anonymous function expression here
function Puppy() {}
Puppy.prototype = new Dog();

(new Puppy()).bark(); // break point alias -> Dog.bark

One more thing, when I discovered this feature, I can't stop thinking of it - does it imply that Chrome will implement ES6 class very soon? How exciting!

Sign up to request clarification or add additional context in comments.

4 Comments

Very helpful answer. I had somehow started thinking that a function assigned to a variable is no longer anonymous and you've corrected my understanding of that. I haven't marked it as correct yet because it's still not clear to me why Chrome dev tools does the a.a.b.b thing exactly. You've convinced me that it doesn't matter much, but do you think this was accidental, or by design?
@foobarbecue I'm not sure why it's a.a.b.b, but I added something to my answer, about how we could take advantage of this feature. Another bold conjecture is that (how exciting), Chrome is going to implement ES6 class very soon?
Interesting... I don't really see why the concatenation thing would imply implementation of class. Can you explain more? My guess is that it is actually some kind of bug in Chrome Dev Tools.
@foobarbecue I found something interesting here code.google.com/p/chromium/issues/detail?id=17356. Read the discussion, especially #44, #49 and #55. So it looks like a feature rather than a bug :D About the implementation of class, it's just my guess. I hope it can get real in near future.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.