Function.prototype.isClassOf(generic)

# Andrea Giammarchi (13 years ago)

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?