简体   繁体   English

为什么javascript会在属性的多个赋值中连接函数名?

[英]Why does javascript concatenate function names in multiple assignment to properties?

This is an edge case and probably bad practice, but it made me curious about some js internals. 这是一个边缘案例,可能是不好的做法,但它让我对一些js内部构件感到好奇。 Can anyone explain why chrome dev tools tells me that I have created a function named aabb here? 谁能解释为什么chrome dev工具告诉我我在这里创建了一个名为aabb的函数?

在此输入图像描述

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': 否则,a和b似乎都引用名为'b'的函数对象:

在此输入图像描述

By the way, I originally encountered this here when trying to answer my own question about dat.gui.js . 顺便说一句,我最初遇到这个在这里要回答的时候我自己的问题有关dat.gui.js。

This has nothing to do with the language spec. 这与语言规范无关。
It's a DevTools enhancement for debugging convenience, which is ported recently in Chrome. 这是一个DevTools增强功能,用于调试方便,最近移植到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. 然后在断点调试中,我们可以从函数调用堆栈中找到名称为_asdf的方法。 Otherwise it's the pain in the ass to do that from a list of (anonymous function) . 否则,从(匿名函数)列表中这样做是痛苦的。

在此输入图像描述

In latest Chrome, when you assign an anonymous function as an object property, an alias will be attached to it. 在最新的Chrome中,当您将匿名函数指定为对象属性时,将为其附加别名。

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 : 请记住,这只是一个DevTools增强功能,该方法仍然是匿名的

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

But it's very helpful in breakpoint debugging: 但它在断点调试中非常有用:

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

在此输入图像描述


EDIT: 编辑:

I'm not very sure why the alias is generated as aabb , it looks very easy but kind of... stupid. 我不太确定为什么别名是作为aabb生成的,它看起来很容易但有点......愚蠢。 However, in practice we seldom do aa = bb = func... thing (lucky). 但是,在实践中我们很少做aa = bb = func... thing(幸运)。 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. 例如,断点中的别名Dog.bark在源代码中清楚地映射到Dog.prototype.bark ,即使它在Puppy实例上调用,我们也不必做旧学校命名的函数表达式。

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? 还有一件事,当我发现这个功能时,我无法停止思考它 - 这是否意味着Chrome很快会实施ES6 课程 How exciting! 多么激动人心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM