如何获取JavaScript类的名称 [英] How to get name of JavaScript Class

查看:81
本文介绍了如何获取JavaScript类的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们来看下面的示例代码:

Let's take the following example code:

var ns = {}; // Some namespace

ns.Test = function()
{
    // Constructor of class Test
};

var inst = new ns.Test();
var className = hereIsTheMagic(inst); // Must return "ns.Test"

所以我在命名空间中创建了一个名为Test的类ns'和这个名为'inst'的类的实例。现在我想找出班级名称。我怎样才能做到这一点?

So I create a class named 'Test' in namespace 'ns' and an instance of this class named 'inst'. Now I want to find out the class name. How can I do this?

到目前为止,我通过给每个类一个带有类名的字符串属性来解决这个问题,所以我可以使用 inst.constructor.className 访问类名。但是如果可能的话我想停止这样做,因为在复制/粘贴类时它很容易出错。

Up to now I solved this problem by giving each class a string property with the class name so I could use inst.constructor.className to access the class name. But if possible I would like to stop doing this because it is pretty error-prone when copy/pasting classes.

如果没有适用于所有当前浏览器的解决方案可能在将来的一些ECMAScript规范中至少有一些新功能可以访问类名吗?

If there is no solution which works in all current browsers maybe there is at least some new feature in some future ECMAScript spec which provides access to class names?

推荐答案

JavaScript没有类,它有构造函数和原型链。它们在一起看起来有点像课程,但它们确实不是。 : - )

JavaScript doesn't have classes, it has constructor functions and the prototype chain. Together, they can look a bit like classes, but they really aren't. :-)

不,没有标准方法 hereIsTheMagic 可以找到你的字符串ns.Test例如,甚至在ECMAScript 5中都没有。首先, ns.Test 可能不是引用该函数的唯一属性(如果添加 ns,该怎么办? .OtherTest = ns.Test; ,例如)。

No, there's no standard way that hereIsTheMagic can find the string "ns.Test" in your example, not even in ECMAScript 5. For one thing, ns.Test may not be the only property referring to that function (what if you added ns.OtherTest = ns.Test;, for instance).

JavaScript确实具有功能的概念名称,但没有标准的方法来访问函数的正确名称,即使你可以,你正在调用的函数 ns.Test 匿名的。您在 ns 上为其指定的属性有一个名称( Test ),但该函数没有。

JavaScript does have the concept of functions having proper names, but there's no standard way to access the proper name of a function, and even if you could, the function you're calling ns.Test is anonymous. The property you've assigned it to on ns has a name (Test), but the function does not.

您可以为该函数命名:

var ns = {}; // Some namespace
(function() {
    ns.Test = ns_Test;

    function ns_Test() {
    // Constructor of class Test
    }
})();

...这是一个好主意,因为它帮助工具帮助您,但JavaScript代码本身没有标准的方法来获取该名称。 (不要做 ns.Test = function ns_Test(){...}; 。它应该可以工作,但ECMAScript有各种各样的bug实现有关它的更多信息,请参阅上面链接的文章。)

...which is a good idea, because it helps tools help you, but there's no standard way for JavaScript code itself to get at that name. (Don't do ns.Test = function ns_Test() { ... };, though. It should work, but there are various implementations of ECMAScript that have various bugs related to it. More about that in the article linked above.)

更新:我可能应该说: - 获取函数正确名称的标准方法(在上面的示例中为 ns_Test )。

Update: I probably should have said: There are non-standard ways to get at the proper name of a function (ns_Test in my example above).

最简单其中包括函数实例上的 name 属性,许多实现支持这些属性,包括:

The easiest of these is the name property on function instances, which is supported by many implementations, including:


  • Firefox的SpiderMonkey

  • Chrome的V8

  • Opera的引擎(我不知道它的名字)

  • Safari的引擎(包括Safari移动版)

  • Firefox's SpiderMonkey
  • Chrome's V8
  • Opera's engine (I don't know its name)
  • Safari's engine (including Safari mobile)

...所以警报(ns_Test.name); 会提醒ns_Test。但它是 - 标准,并且不受任何版本的JScript(微软的引擎)的支持,甚至不是IE9使用的版本(现在终于发布了!)。这是一个快速的测试页面: http://jsbin.com/oxuva3

...so alert(ns_Test.name); would alert "ns_Test". But it is non-standard and not supported by any version of JScript (Microsoft's engine), not even the version used by IE9 (now finally released!). Here's a quick tester page: http://jsbin.com/oxuva3

请注意,这也是函数的正确名称(请参阅上面的示例),而不是您为其指定的属性的名称。

Note that, again that's the proper name of the function (see my example above), not the name of the property you've assigned it to.

如果 name 不存在,则可以在函数对象上使用 toString()可以(或可能不)返回函数的字符串表示(例如,function ns_Test(){...})。这完全是非标准的,但在桌面浏览器上很常见;它在几个移动浏览器上不起作用,在任何情况下都可能是个坏主意。 : - )

If name isn't there, you can use toString() on the function object, which may (or may not) return a string representation of the function (e.g., "function ns_Test() { ... }"). That's completely non-standard, but common on desktop browsers; it doesn't work on several mobile browsers, and probably a bad idea in any case. :-)

这篇关于如何获取JavaScript类的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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