Function.prototype.isClassOf(generic)
Andrea Giammarchi
andrea.giammarchi at gmail.com
Wed Feb 27 10:34:50 PST 2013
Surely already discussed, I forgot the outcome.
Why we have Array.isArray but not a more generic Function.isClassOf() ?
Here an absolutely not optimized idea of what I am talking about:
Function.prototype.isClassOf = function isClassOf(obj) {
if (obj == null) return false;
var Class = {}.toString.call(obj);
return Class == '[object ' + this.name + ']' || (
Class == '[object Object]' &&
obj instanceof this
);
};
function UserDefined() {}
alert([
Array.isClassOf([]), // true
Object.isClassOf({}), // true
Function.isClassOf(function(){}), // true
Boolean.isClassOf(false), // true
UserDefined.isClassOf(new UserDefined), // true
Object.isClassOf([]) // false
].join('\n'));
If the answer is about not polluting prototypes, I wonder how many
developers out there for/in functions, 'cause if they do, they have to
check own property and remove 'length' and 'name' anyhow.
Thoughts?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130227/056b3566/attachment.html>
More information about the es-discuss
mailing list