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

How does JavaScript determine an array

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to judge arrays by JavaScript". In daily operation, I believe many people have doubts about how to judge arrays by JavaScript. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to judge arrays by JavaScript". Next, please follow the editor to study!

Judging instanceof according to constructor

Determine whether an instance belongs to a constructor

Let arr = [] console.log (arr instanceof Array) / / true

Disadvantages: the underlying principle of instanceof is to detect whether the prototype property of the constructor appears on the prototype chain of an instance. If the prototype chain of the instance changes, a correct judgment cannot be made.

Let arr = [] arr.__proto__ = function () {} console.log (arr instanceof Array) / / falseconstructor

The constructor property constructor of the instance points to the constructor itself.

Let arr = [] console.log (arr.constructor = Array) / / true

Cons: if the constructor of the arr is modified, a correct judgment cannot be made.

Let arr = [] arr.constructor = function () {} console.log (arr.constructor = Array) / / false determines _ _ proto _ _ based on the prototype object

The _ _ proto _ _ of the instance points to the prototype object of the constructor

Let arr = [] console.log (arr.__proto__ = Array.prototype) / / true

Disadvantages: if the prototype chain of the instance is modified, a correct judgment cannot be made.

Let arr = [] arr.__proto__ = function () {} console.log (arr.__proto__ = Array.prototype) / / falseObject.getPrototypeOf ()

The method that comes with Object to get the prototype object to which an object belongs

Let arr = [] console.log (Object.getPrototypeOf (arr) = Array.prototype) / / true

Disadvantages: same as above

Array.prototype.isPrototypeOf ()

The method of Array prototype object to determine whether it is the prototype object of an object

Let arr = [] console.log (Array.prototype.isPrototypeOf (arr)) / / true

Disadvantages: same as above

Judge Object.prototype.toString.call () according to the prototype object of Object

There is a toString method on the prototype object of Object, and the toString method is inherited by all objects by default, returning the "[object type]" string. However, this method is often overridden by methods of the same name on the prototype chain and needs to be forcibly called through Object.prototype.toString.call ().

Let arr = [] console.log (Object.prototype.toString.call (arr) ='[object Array]') / / true

This type is like a birthmark, engraved on the body at birth, so modifying the prototype chain will not have any effect on it.

Let arr = [] arr.__proto__ = function () {} console.log (Object.prototype.toString.call (arr) = ='[object Array]') / / trueArray.isArray ()

Array.isArray () is a new method added by ES6, which is specially used for array type judgment, and the principle is the same as above.

Let arr = [] console.log (Array.isArray (arr)) / / true

Modifying the prototype chain will not affect it in any way.

Let arr = [] arr.__proto__ = function () {} console.log (Array.isArray (arr)) / / true here, the study of "how JavaScript judges arrays" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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