How to use JavaScript to determine variable types based on user-defined functions

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.