JavaScript: Implementation of variable types based on user-defined functions this article describes how to use JavaScript to determine variable types based on user-defined functions. We will share this with you for your reference. The details are as follows:
Generally, typeof is used to determine the js variable type, but many times only typeof can not meet the requirements.
I wrote a UDF to do this, and the judgment is comprehensive.
Function varType (v) {if (typeof v = "object") {if (v = null) return 'null'; if (v. constructor) return (v. constructor. toString ()). match (/(? :) [/W/$] +/) [0]; if (typeof typeof2 === 'undefined' & window. execScript) {window. execScript ('function vbsTypeName (o): vbsTypeName = TypeName (o): End function', 'vbscript'); window. execScript ('function typeof2 (o) {return vbsTypeName (o)} ', 'jscript');} if (typeof typeof2! = 'Undefined') {return typeof2 (v);} return "object";} return typeof v;} // for common js constants and js objects, alert (varType (); // undefinedalert (varType (100); // numberalert (varType ({})); // Objectalert (varType ([]); // Arrayalert (varType (//); // RegExpalert (varType (new Date ())); // Datealert (varType (Date); // functionalert (varType (Object); // functionalert (varType (RegExp); // function // for DOM objects, different browsers may have different values: alert (varType (window); // IE: HTMLWindow2 FF: Invalid walert (varType (document); // IE: HTMLDocument FF: HTMLDocumentalert (varType (document. body); // IE: HTMLBody FF: HTMLBodyElementalert (varType (Option); // IE: Object FF: functionalert (varType (Image); // IE: object FF: functionalert (varType (navigator); // IE: DispHTMLNavigator FF: Navigator // The following are applicable only to IE. Other kernel Browsers Do not support alert (varType (ActiveXObject )); // IE: functionalert (varType (Enumerator); // IE: functionalert (varType (new ActiveXObject ("Scripting. dictionary "); // IE: Dictionaryalert (varType (new Enumerator (); // IE: Enumerator