DevTools 如何确定对象的构造函数名称? [英] How does DevTools determine an object's constructor's name?

查看:23
本文介绍了DevTools 如何确定对象的构造函数名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Chrome v22.0.1229.94 m,这并不重要,但以防万一:-)

I'm using Chrome v22.0.1229.94 m, not that it should matter, but just in case :-)

便携性对我来说并不重要.只要它适用于 Chrome(和 Safari),我就很高兴.

我有两个函数,foo 和 bar:

I have two functions, foo and bar:

var foo = function() {
  ...
}
function bar() {
  ...
}

然后如果我使用它们创建对象:

then if I create objects using them:

f1 = new foo();
b1 = new bar();

我可以获取 bar 的构造函数名称,但不能获取 foo 的构造函数名称:

I can get the constructor's name for bar, but nor for foo:

> f1.constructor.name  ==> ""
> b1.constructor.name  ==> "bar"

但在这两种情况下,当我检查变量 f1b1 时,DevTools 控制台都会显示名称foo"和bar".

yet in both cases DevTools console will show the names "foo" and "bar" when I inspect the variables f1 and b1.

如何像 DevTools 那样为 f1 提取 name foo ?

How do I extract the name foo for f1 as DevTools is able to?

推荐答案

V8 可以在解析阶段执行函数名称推断,并通过其 API 公开此信息.您将无法使用普通对象属性提取此信息.

V8 can perform function name inference at the parsing stage, exposing this information via its APIs. You will not be able to extract this info using the normal object properties.

这篇关于DevTools 如何确定对象的构造函数名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆