我遇到了 Extends 的问题,但找到了解决方法:
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);
如果您遵循以下模式,则可以避免检查扩展名称:
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();