如何从内部获取Mootools类的名称 [英] How to get the name of a Mootools class from within

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

问题描述

我想得到类的变量名。

var Poop = new Class({ 
    getClassName: function() { 
        return arguments.callee._owner.name;
    } 
});
var a = new Poop();
a.getClassName(); //want 'Poop'

我正在制作它将被实现到其他类中,而且我' d想要构建一个使用表名的类名(复数)的SQL查询。

I'm making that will be implemented into other classes, and I'd like to build a SQL query that uses the class name (pluralized) for the table.

我尝试了以上示例的各种组合来尝试获取名称,并且无法弄明白(如果甚至可以考虑MooTools类系统)。

I've tried various combinations of the above example to try to get the name, and can't figure it out (if it's even possible considering MooTools class system).

推荐答案

找到解决方案。 Hash有一个keyOf函数,它将为我提供变量名称一个值。所以我做了一个Hash of window,然后询问了类构造函数的键。

Found a solution. Hash has a keyOf function, which will give me the variable name that holds a value. So I made a Hash of window, and then asked for the key of the class constructor.

var Poop = new Class({
    name: function() {
        var w = $H(window);
        return w.keyOf(this.constructor);
    }
});
var a = new Poop();
a.name(); // returns 'Poop'

这当然有效,因为我在全局窗口命名空间中创建了类(与MooTools一样常见)。如果您在某个命名空间中创建了类,那么您只需要改为使用Hash该命名空间。

This of course works only because I create classes in the global window namespace (as is common with MooTools). If you made classes inside some namespace, then you'd just need to Hash that namespace instead.

编辑:我写了关于如何使用这个解决方案以及如何使它在名称空间中自动工作,对任何感兴趣的MooToolers来说。

I wrote up about how to use this solution and how to make it work in namespaces automatically, for any MooToolers interested.

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

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