admin管理员组文章数量:1358338
I'd like to get at the variable name of class.
var Poop = new Class({
getClassName: function() {
return arguments.callee._owner.name;
}
});
var a = new Poop();
a.getClassName(); //want 'Poop'
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.
I've tried various binations 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).
I'd like to get at the variable name of class.
var Poop = new Class({
getClassName: function() {
return arguments.callee._owner.name;
}
});
var a = new Poop();
a.getClassName(); //want 'Poop'
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.
I've tried various binations 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).
Share Improve this question asked May 8, 2009 at 0:39 seanmonstarseanmonstar 11.5k2 gold badges22 silver badges26 bronze badges3 Answers
Reset to default 8Found 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'
This of course works only because I create classes in the global window namespace (as is mon with MooTools). If you made classes inside some namespace, then you'd just need to Hash that namespace instead.
Edit: I wrote up about how to use this solution and how to make it work in namespaces automatically, for any MooToolers interested.
I ran into a problem with Extends, but found a way around it:
var getClassNameFromKeys = function (obj, instance) {
var i = 0, keys = Object.keys(obj);
for (i; i < keys.length; i++) {
if ('Ext' !== keys[i] && typeOf(obj[keys[i]]) === 'class' && instance instanceof obj[keys[i]]) {
return keys[i];
}
}
},
Ext = new Class(),
WhatsMyName = new Class({
Extends: Ext
}),
whatsMyNameInstance = new WhatsMyName(),
cName = getClassNameFromKeys(this, whatsMyNameInstance);
console.log(cName);
You can avoid the check on the extends name if you follow a pattern something like this:
var O = {},
whatsMyNameInstance;
(function () {
var SUBCLASS_KEYS;
O.Ext = new Class({
_className: null,
getClassName: function () {
var i = 0;
SUBCLASS_KEYS = SUBCLASS_KEYS || Object.keys(O.subclasses);
for (i; i < SUBCLASS_KEYS.length; i++) {
if (this instanceof O.subclasses[SUBCLASS_KEYS[i]]) {
return SUBCLASS_KEYS[i];
}
}
}
});
})();
O.subclasses = {};
O.subclasses.WhatsMyName = new Class({
Extends: O.Ext
});
whatsMyNameInstance = new O.subclasses.WhatsMyName();
whatsMyNameInstance.getClassName();
I don't think this is possible in Javascript, due to the prototype-oriented nature of the language. There are several things you can do to determine whether an object is of an existing class that you know, such as:
if (a.constructor == Poop) {
alert("This will work, but I don't really want any Poop.");
}
However, that doesn't really work for determining an unknown object's class. There's other prototype things you can do to check for class that involve toString()
, but they only work for built-in objects, not custom classes, and this is a drawback of prototyping that's not specific to MooTools.
If you check out the 5th Edition of Javascript, The Definitive Guide, page 174, chapter 9.7, there's a nice discussion on it there. Basically the remendation is to populate your custom classes with a classname
property, and override the default base Object class toString()
method to check this property when you're calling it on a custom class.
本文标签: javascriptHow to get the name of a Mootools class from withinStack Overflow
版权声明:本文标题:javascript - How to get the name of a Mootools class from within - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743898855a2558298.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论