Home Web Front-end JS Tutorial Judgment type in JS

Judgment type in JS

Apr 10, 2018 am 11:38 AM
javascript judgment type

The content shared with you in this article is about the judgment type in JS, which has certain reference value. Friends in need can refer to it

There are generally four methods to judge the type in JS
1,
typeof: As everyone knows about this method, there are many types that cannot be determined, and it is not recommended.
2,
constructor: Each function will automatically have a prototype attribute. This property is an object, and this object contains the only non-enumerable property constructor. The value of the constructor attribute is a function object, and this function is the constructor;

function Aaa(){};
var a1 = new Aaa();

a1.constructor points to It is Aaa(); this constructor

So we can use it to do type judgment

var arr = [];
arr.constructor == Array This way we can judge whether it is Array, the same is true for other types


instanceof: Determine whether an object and the constructor are on a prototype chain;

function Aaa(){};
var o = new Aaa();

o instanceof Array : Check whether o and Array are on the same prototype chain. Obviously o is an instance under the Aaa constructor, so we get false;

o instanceof obj.prototype gets true; because all objects inherit obj.prototype

4,
Use the toString() method: Object.prototype.toString.call(o);
o : is an object instance. The above will return a string of type '[Object Array]'. You can judge the data type based on this string.

Summary, the fourth general method is the most reliable and safe. , Methods two and three generally have no problems, but they will go wrong when there is an iframe. One is the most unreliable

There are generally four methods to determine the type in JS
One,
typeof: Everyone knows this method. There are many types that cannot be determined. It is not recommended.
2.
constructor: Each function will automatically have a prototype attribute. This property is an object, and this object contains the only non-enumerable property constructor. The value of the constructor attribute is a function object, and this function is the constructor;

function Aaa(){};
var a1 = new Aaa();

a1.constructor points to It is Aaa(); this constructor

So we can use it to do type judgment

var arr = [];
arr.constructor == Array This way we can judge whether it is Array, the same is true for other types


instanceof: Determine whether an object and the constructor are on a prototype chain;

function Aaa(){};
var o = new Aaa();

o instanceof Array : Check whether o and Array are on the same prototype chain. Obviously o is an instance under the Aaa constructor, so we get false;

o instanceof obj.prototype gets true; because all objects inherit obj.prototype

4,
Use the toString() method: Object.prototype.toString.call(o);
o : is an object instance. The above will return a string of type '[Object Array]'. You can judge the data type based on this string.

Summary, the fourth general method is the most reliable and safe. , the second and third methods generally have no problems, but they will go wrong when there is an iframe. The first is the most unreliable

Related recommendations;

js judgment is on the PC side Or mobile terminal

js determine whether it is PC terminal or mobile terminal

The above is the detailed content of Judgment type in JS. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to create a video matrix account? What types of matrix accounts do it have? How to create a video matrix account? What types of matrix accounts do it have? Mar 21, 2024 pm 04:57 PM

With the popularity of short video platforms, video matrix account marketing has become an emerging marketing method. By creating and managing multiple accounts on different platforms, businesses and individuals can achieve goals such as brand promotion, fan growth, and product sales. This article will discuss how to effectively use video matrix accounts and introduce different types of video matrix accounts. 1. How to create a video matrix account? To make a good video matrix account, you need to follow the following steps: First, you must clarify what the goal of your video matrix account is, whether it is for brand communication, fan growth or product sales. Having clear goals helps develop strategies accordingly. 2. Choose a platform: Choose an appropriate short video platform based on your target audience. The current mainstream short video platforms include Douyin, Kuaishou, Huoshan Video, etc.

How to determine whether a date is the previous day in Go language? How to determine whether a date is the previous day in Go language? Mar 24, 2024 am 10:09 AM

Question: How to determine whether the date is the previous day in Go language? In daily development, we often encounter situations where we need to determine whether the date is the previous day. In the Go language, we can implement this function through time calculation. The following will be combined with specific code examples to demonstrate how to determine whether the date is the previous day in Go language. First, we need to import the time package in the Go language. The code is as follows: import("time") Then, we define a function IsYest

jQuery usage practice: several ways to determine whether a variable is empty jQuery usage practice: several ways to determine whether a variable is empty Feb 27, 2024 pm 04:12 PM

jQuery is a JavaScript library widely used in web development. It provides many simple and convenient methods to operate web page elements and handle events. In actual development, we often encounter situations where we need to determine whether a variable is empty. This article will introduce several common methods of using jQuery to determine whether a variable is empty, and attach specific code examples. Method 1: Use the if statement to determine varstr="";if(str){co

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

What is the type of return value of Golang function? What is the type of return value of Golang function? Apr 13, 2024 pm 05:42 PM

Go functions can return multiple values ​​of different types. The return value type is specified in the function signature and returned through the return statement. For example, a function can return an integer and a string: funcgetDetails()(int,string). In practice, a function that calculates the area of ​​a circle can return the area and an optional error: funccircleArea(radiusfloat64)(float64,error). Note: If the function signature does not specify a type, a null value is returned; it is recommended to use a return statement with an explicit type declaration to improve readability.

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

C++ function types and characteristics C++ function types and characteristics Apr 11, 2024 pm 03:30 PM

C++ functions have the following types: simple functions, const functions, static functions, and virtual functions; features include: inline functions, default parameters, reference returns, and overloaded functions. For example, the calculateArea function uses π to calculate the area of ​​a circle of a given radius and returns it as output.

See all articles