


A brief discussion on data type detection in javascript_javascript skills
Javascript data is divided into two types: simple data and complex data. Simple data includes five types: number, string, boolean, undefined and null; there is only one type of complex data, object. [Friendly thanks to Teacher Li Zhan here, <
2. JavaScript data type detection
1. The universal typeof
Let’s first test how to obtain simple data types through typeof. Don’t say anything, the code is king:
/ / Get the data type of variable obj
function getType(obj) {
return typeof (obj);
}
/*Constant get type*/
alert(getType(1)); //number
alert(getType("jeff wong")); //string
alert(getType(true)); //boolean
alert(getType(undefined)); //undefined
alert(getType(null)); //object
/*Variable get type*/
var num = 1;
var str = "jeff wong";
var flag = true;
var hell = undefined;
var none = null;
alert(getType(num)); //number
alert(getType(str)); //string
alert(getType( flag)); //boolean
alert(getType(hell)); //undefined
alert(getType(none)); //object
As you can see In that way, through the typeof operator, the first four simple data types are completely expected, but typeof null returns object. It should be noted that null is the only value of the null type, but null is not an object, and a variable with a null value is not an object, so the null type cannot be correctly obtained directly through typeof. To correctly obtain simple data types, just add some improvements to getType:
function getType(obj) {
return (obj === null) ? "null" : typeof (obj);
}
Let’s try it next Complex data type object:
function Cat() {
}
Cat.prototype.CatchMouse = function () {
//do some thing
}
// Get the data type of variable obj
function getType(obj) {
return (obj === null) ? "null" : typeof (obj);
}
var obj = new Object();
alert(getType(obj)); //object
var func = new Function();
alert(getType(func)); //function
var str = new String("jeff wong");
alert(getType(str)); // object
var num = new Number(10);
alert(getType(num)); //object
var time = new Date();
alert(getType(time)); / /object
var arr = new Array();
alert(getType(arr)); //object
var reg = new RegExp();
alert(getType(reg)); / /object
var garfield = new Cat();
alert(getType(garfield)); //object
We see that except Function (please note the case) is returned Function, whether it is the common built-in objects of JavaScript such as Object, String or Date, etc., or a custom function, all returned by typeof are all objects without exception. But for custom functions, we prefer to get its "true face" (in the example, Cat, not object), and obviously, typeof does not have this conversion processing capability.
2. Constructor, I want to say I love you loudly
Since the universal typeof sometimes has no solution, how do we judge whether a variable is a custom function instance? We know that all objects in JavaScript have a constructor attribute. This attribute can help us determine the object data type, especially for custom functions:
var obj = "jeff wong";
alert(obj.constructor == String); //true
obj = new Cat();
alert(obj.constructor == Cat); //true
However, you can also test it with the following code:
//alert(1.constructor); //Numeric constant error Numeric constant has no constructor
var num = 1;
alert(num.constructor == Number); //true
alert("jeff wong".constructor == String); //true
var str = "jeff wong";
alert(str.constructor == String); //true
var obj= null;
alert(obj.constructor); //null has no constructor attribute
none = undefined;
alert(obj.constructor); //undefined has no constructor attribute
Experiments have shown that numeric constants, null and undefined do not have constructor attributes.
At this point, will you be as happy as Lou Zhu and think it’s finally done? The following code may also be of some inspiration and excavation:
function Animal() {
}
function Cat() {
}
Cat.prototype = new Animal();
Cat.prototype.CatchMouse = function () {
/ /do some thing
}
var obj = new Cat();
alert(obj.constructor == Cat); //false? ?
alert(obj.constructor == Animal); //true understanding
It turns out that constuctor is not so easy to use in the case of prototype chain inheritance. What to do?
3. Intuitive instanceof
Hey, please instanceof makes a grand appearance. Judging from its naming, it seems to be to obtain an instance of a certain object. I don’t know if this is the right way to understand it? Anyway, let’s improve the above code and test it first:
function Animal() {
}
function Cat() {
}
Cat.prototype = new Animal();
Cat.prototype.CatchMouse = function () {
//do some thing
}
var garfield = new Cat();
alert(garfield instanceof Cat); //true no doubt
alert(garfield instanceof Animal); //true It’s understandable
Okay, regarding the data type detection of JavaScript, Louzhu has summarized it here. I hope someone with a heart can add more.

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











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

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

When designing database tables, choosing the appropriate data type is very important for performance optimization and data storage efficiency. In the MySQL database, there is really no so-called best choice for the data type to store the gender field, because the gender field generally only has two values: male or female. But for efficiency and space saving, we can choose a suitable data type to store the gender field. In MySQL, the most commonly used data type to store gender fields is the enumeration type. An enumeration type is a data type that can limit the value of a field to a limited set.

Title: Basic Data Types Revealed: Understand the Classifications in Mainstream Programming Languages Text: In various programming languages, data types are a very important concept, which defines the different types of data that can be used in programs. For programmers, understanding the basic data types in mainstream programming languages is the first step in building a solid programming foundation. Currently, most major programming languages support some basic data types, which may vary between languages, but the main concepts are similar. These basic data types are usually divided into several categories, including integers

C language is a widely used computer programming language that is efficient, flexible and powerful. To be proficient in programming in C language, you first need to understand its basic syntax and data types. This article will introduce the basic syntax and data types of C language and give examples. 1. Basic syntax 1.1 Comments In C language, comments can be used to explain the code to facilitate understanding and maintenance. Comments can be divided into single-line comments and multi-line comments. //This is a single-line comment/*This is a multi-line comment*/1.2 Keyword C language

What basic knowledge do you need to know before learning Python? With the continuous development of technologies such as artificial intelligence, big data and cloud computing, programming has become an increasingly important skill in modern society. As a simple, easy-to-learn and powerful programming language, Python is increasingly favored by programmers and beginners. If you also plan to learn Python, there are some basic knowledge that you must master before starting. Understand the basic concepts of programming. Before starting to learn any programming language, you first need to understand some basic concepts.
