Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between typeof and instanceof

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about the differences between typeof and instanceof. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's learn about it.

Typeof

Typeof is a unary operator that precedes an Operand, which can be of any type. It returns a string indicating the type of Operand. Please look at Chestnut:

Const type = typeof 'long live China'; / / stringtypeof 666; / / numbertypeof true; / / booleantypeof undefined; / / undefinedtypeof Symbol (); / / symboltypeof 1n; / / biginttypeof () = > {}; / / functiontypeof []; / / objecttypeof {}; / / objecttypeof new String ('xxx'); / / objecttypeof null; / / object

As can be seen from the above examples, typeof can only accurately judge basic data types and functions (functions are actually objects and do not belong to another data type, but can also be distinguished by using typeof), but cannot accurately determine reference data types (all return object).

It is important to note that the call to typeof null returns object because the special value null is considered a reference to a null object (also called a null object pointer).

If you want to accurately determine the reference data type, you can use the instanceof operator.

Instanceof

The instanceof operator is placed after an Operand and before a given object. It returns a Boolean value indicating whether the Operand is an instance of a given object:

Const result = [] instanceof Array; / / trueconst Person = function () {}; const p = new Person (); p instanceof Person; / / trueconst message = new String ('xxx'); message instanceof String; / / true distinction

Typeof returns the basic type of an Operand, while instanceof returns a Boolean value.

Instanceof can accurately judge the reference data type, but can not correctly judge the basic data type

Although typeof can determine the basic data type (except null), it cannot determine the reference data type (except function)

Extend Object.prototype.toString.call ()

Both typeof and instanceof have some drawbacks and can not meet the needs of all scenarios. If you need a generic test data type, you can use the Object.prototype.toString.call () method:

Object.prototype.toString.call ({}); / "[object Object]" Object.prototype.toString.call ([]); / / "[object Array]" Object.prototype.toString.call (666); / / "[object Number]" Object.prototype.toString.call ('xxx'); / / "[object String]"

Notice that this method returns a string of the format "[object Object]".

Encapsulated function

For easier use, we can encapsulate this method:

Function getType (value) {let type = typeof value; if (type! = = 'object') {/ / if it is a basic data type, return return type;} / / if it is a reference data type, and then make a further judgment, the regular return result return Object.prototype.toString.call (value). Replace (/ ^\ [object (\ S+)\] $/,' $1');} getType (123) / / numbergetType ('xxx'); / / stringgetType (() = > {}); / / functiongetType ([]); / / ArraygetType ({}); / / ObjectgetType (null); / / Null is all about the article "what's the difference between typeof and instanceof". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report