JavaScript data type verification method_javascript skills
Recently, I have a new understanding of the verification of JavaScript data types. It turns out that it can be so simple and so comprehensive.
We have customized isString, isNumber, isDate, isError, isRegExp, isBoolean, isNull, isUndefined, isObject and other methods. Now show the javascript data type verification function and test set you defined:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> </body> <script type="text/javascript"> //isString //isNumber //isDate //isError //isRegExp //直接利用和数据类型来判断 [].forEach.call(['String','Number','Date','Error','RegExp'],function(name){ this['is'+name]=function(obj){ return toString.call(obj)==='[object '+name+']'; }; }); //isBoolean //true和false需要考虑在内 Object.prototype.isBoolean=function(obj){ return obj===true||obj===false||toString.call(obj)==='[object Boolean]'; }; //isNull //未找到所指向对象 Object.prototype.isNull=function(obj){ return obj===null; }; //isUndefined //定义了但是未赋值 Object.prototype.isUndefined=function(obj){ return obj===void 0; }; //isObject //函数和数组都是对象 Object.prototype.isObject=function(obj){ var type = typeof obj; return type === 'function' || type === 'object' && !!obj; }; //test //isString var str="iamstring"; var a=isString(str); console.log(a);//true //isNumber var b=isNumber(a); console.log(b);//false //isNumber var num=4; var c=isNumber(num); console.log(c);//true //isRegExp var reg=/^[1-9]/; var d=isRegExp(reg); console.log(d);//true //isDate var date=new Date(); var e=isDate(date); console.log(e);//true //isBoolean var bool=false; var f=isBoolean(bool); console.log(f);//true //isNull var nul=document.getElementById("div02"); var g=isNull(nul); console.log(g);//true //isUndefined var undef; var h=isUndefined(undef); console.log(h);//true //isObject var obj={"1":"1","2":"2"}; var i=isObject(obj); console.log(i);//true </script> </html>
The following is a detailed introduction to judging the data type of JavaScript, which is divided into six data types. Friends in need can come and refer to it. I hope it will be helpful to everyone
1. Determine whether it is an array type
The code is as follows:
<STRONG><script type="text/javascript"> //<![CDATA[ var a=[0]; document.write(isArray(a),'<br/>'); function isArray(obj){ return (typeof obj=='object')&&obj.constructor==Array; } //]]> </script></STRONG>
2 Determine whether it is a string type
The code is as follows:
<script type="text/javascript"> //<![CDATA[ document.write(isString('test'),'<br/>'); document.write(isString(10),'<br/>'); function isString(str){ return (typeof str=='string')&&str.constructor==String; } //]]> </script>
3 Determine whether it is a numeric type
The code is as follows:
<script type="text/javascript"> //<![CDATA[ document.write(isNumber('test'),'<br/>'); document.write(isNumber(10),'<br/>'); function isNumber(obj){ return (typeof obj=='number')&&obj.constructor==Number; } //]]> </script>
4 Determine whether it is date type
The code is as follows:
<script type="text/javascript"> //<![CDATA[ document.write(isDate(new Date()),'<br/>'); document.write(isDate(10),'<br/>'); function isDate(obj){ return (typeof obj=='object')&&obj.constructor==Date; } //]]> </script>
5 Determine whether it is a function
The code is as follows:
<script type="text/javascript"> //<![CDATA[ document.write(isFunction(function test(){}),'<br/>'); document.write(isFunction(10),'<br/>'); function isFunction(obj){ return (typeof obj=='function')&&obj.constructor==Function; } //]]> </script>
6 Determine whether it is an object
The code is as follows:
<script type="text/javascript"> linenum //<![CDATA[ document.write(isObject(new Object()),'<br/>'); document.write(isObject(10),'<br/>'); function isObject(obj){ return (typeof obj=='object')&&obj.constructor==Object; } //]]> </script>
I hope this article will be helpful to everyone learning JavaScript programming.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

We usually receive PDF files from the government or other agencies, some with digital signatures. After verifying the signature, we see the SignatureValid message and a green check mark. If the signature is not verified, the validity is unknown. Verifying signatures is important, let’s see how to do it in PDF. How to Verify Signatures in PDF Verifying signatures in PDF format makes it more trustworthy and the document more likely to be accepted. You can verify signatures in PDF documents in the following ways. Open the PDF in Adobe Reader Right-click the signature and select Show Signature Properties Click the Show Signer Certificate button Add the signature to the Trusted Certificates list from the Trust tab Click Verify Signature to complete the verification Let

1. After opening WeChat, click the search icon, enter WeChat team, and click the service below to enter. 2. After entering, click the self-service tool option in the lower left corner. 3. After clicking, in the options above, click the option of unblocking/appealing for auxiliary verification.

In a MySQL database, gender fields can usually be stored using the ENUM type. ENUM is an enumeration type that allows us to select one as the value of a field from a set of predefined values. ENUM is a good choice when representing a fixed and limited option like gender. Let's look at a specific code example: Suppose we have a table called "users" that contains user information, including gender. Now we want to create a field for gender, we can design the table structure like this: CRE

PHP8 is the latest version of PHP, bringing more convenience and functionality to programmers. This version has a special focus on security and performance, and one of the noteworthy new features is the addition of verification and signing capabilities. In this article, we'll take a closer look at these new features and their uses. Verification and signing are very important security concepts in computer science. They are often used to ensure that the data transmitted is complete and authentic. Verification and signatures become even more important when dealing with online transactions and sensitive information because if someone is able to tamper with the data, it could potentially

In MySQL, the most suitable data type for gender fields is the ENUM enumeration type. The ENUM enumeration type is a data type that allows the definition of a set of possible values. The gender field is suitable for using the ENUM type because gender usually only has two values, namely male and female. Next, I will use specific code examples to show how to create a gender field in MySQL and use the ENUM enumeration type to store gender information. The following are the steps: First, create a table named users in MySQL, including

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Detailed explanation of how to use Boolean types in MySQL MySQL is a commonly used relational database management system. In practical applications, it is often necessary to use Boolean types to represent logical true and false values. There are two representation methods of Boolean type in MySQL: TINYINT(1) and BOOL. This article will introduce in detail the use of Boolean types in MySQL, including the definition, assignment, query and modification of Boolean types, and explain it with specific code examples. 1. The Boolean type is defined in MySQL and can be

Steam is a platform used by game enthusiasts. You can buy and purchase many games here. However, recently many users have been stuck in the mobile token verification interface when logging into Steam and cannot log in successfully. Faced with this Most users don't know how to solve this situation. It doesn't matter. Today's software tutorial is here to answer the questions for users. Friends in need can check out the operation methods. Steam mobile token error? Solution 1: For software problems, first find the steam software settings on the mobile phone, request assistance page, and confirm that the network using the device is running normally, click OK again, click Send SMS, you can receive the verification code on the mobile phone page, and you are done. Verify, resolve when processing a request
