The safest way is to check the internal [[Class]] property by setting the object as the thisArg argument of the .call()
method when calling Object.prototype.toString
.
Object.prototype.toString.call( myVariable ) === '[object Function]';
Of course you could easily make a function out of it:
function checkClass( obj ) {
return Object.prototype.toString.call( obj ).slice( 8, -1).toLowerCase();
}
checkClass( myVariable ) === 'function';
This is very simple, and there could be some improvements, but you get the idea.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…