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 es6 determine whether a variable is an array?

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

Share

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

Xiaobian to share with you how es6 judges whether variables are arrays, I believe most people still do not know how, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

In es6, you can use the isArray() method of Array to determine whether a variable is an array, which is used to determine whether an object is an array type, syntax "Array.isArray(obj)"; if the object is an array, return true, otherwise return false.

Operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

Determine whether a variable is an array in ES5

In ES5, there are at least five ways to determine whether a value is an array:

var a = [];// 1. Based on instanceof an instanceof Array; // 2. Based on constructor a.constructor == Array; // 3. Based on Object.prototype.isPrototypeOf Array.prototype. isPrototypeOf(a); // 4. Based on getPrototypeOf Object.getPrototypeOf(a) == Array.prototype; // 5. Based on Object.prototype.toString Object.prototype.toString.apply(a) == '[object Array]';

Above, except for Object.prototype.toString, other methods cannot correctly determine the type of variable.

Be aware that the environment in which code runs is complex, and that a variable may use all sorts of tricks to confuse its creator. and look at

var a = { __proto__: Array.prototype};//Try running the following code on the console respectively// 1. Based on instanceof an instanceof Array; // => true // 2. Based on constructor a.constructor == Array; // => true // 3. Based on Object.prototype. isPrototypeOf(a); // => true // 4. Based on getPrototypeOf(a) == Array.prototype; // => true

All four methods return true, why? We just manually specify the__proto__attribute of an object as Array.prototype, which causes the object to inherit the Array object. This irresponsible inheritance method makes the judgment scheme based on inheritance collapse instantly.

Not only that, but we also know that Array is heap data, and variables point to only its reference address, so the address referenced by the Array object of each page is different. An array declared in an iframe whose constructor is the Array object in the iframe. If you declare an array x in the iframe and assign it to the variable y on the parent page, then using y instanceof Array on the parent page must result in false. The last type returns a string and has no reference problems. In fact, only strings can interact freely between multiple pages or systems.

Determine whether a variable is an array in ES6

In view of the commonness of arrays, the Array.isArray method has been added in ES6. Using this method to determine whether a variable is an array is very simple, as follows:

Array.isArray([]); // => true Array.isArray({0: 'a', length: 1}); // => false

In fact, judging the type of a value through Object.prototype.toString is also the standard of major mainstream libraries. So Array.isArray's polyfill usually looks like this:

if (! Array.isArray){ Array.isArray = function(arg){ return Object.prototype.toString.call(arg) === '[object Array]'; }; } The above is "es6 how to determine whether a variable is an array" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to 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