Why does javascript concatenate function names in multiple assignment to properties? -


this edge case , bad practice, made me curious js internals. can explain why chrome dev tools tells me have created function named a.a.b.b here?

enter image description here

note not happen unless assigning property. otherwise both , b appear refer function object named 'b':

enter image description here

by way, encountered here when trying answer my own question dat.gui.js .

this has nothing language spec.
it's devtools enhancement debugging convenience, ported in chrome.

remember used do?

function f() {} // notice it's named function expression f.prototype.asdf = function _asdf() { debugger; };   var f = new f(); f.asdf(); 

then in breakpoint debugging, can find method name _asdf function call stack. otherwise it's pain in ass list of (anonymous function).

enter image description here

in latest chrome, when assign anonymous function object property, alias attached it.

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

remember, it's devtools enhancement, method remains anonymous:

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

but it's helpful in breakpoint debugging:

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

enter image description here


edit:

i'm not sure why alias generated a.a.b.b, looks easy kind of... stupid. however, in practice seldom a.a = b.b = func... thing (lucky). instead, define method in 1 place, , inheritence when necessary, rather copy reference directly.

so in programming practice, alias should , reflect define method. example, alias dog.bark in breakpoint maps dog.prototype.bark in source code, if it's called on puppy instance, , don't have 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 discovered feature, can't stop thinking of - imply chrome implement es6 class soon? how exciting!


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -