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 to understand pseudo arrays in javascript

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

Share

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

This article mainly explains "how to understand pseudo arrays in javascript". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand pseudo arrays in javascript".

In javascript, pseudo-array, also known as class array, is an array-like object, which stores data according to index and has length attribute; because it is an object, pseudo-array does not have push (), forEach () and other methods of array.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Definition and characteristics of pseudo array

Pseudo array (ArrayLike), also known as class array. Is an array-like object that stores data by index and has a length property. But there are several characteristics.

Store data indexed

0: xxx, 1: xxx, 2: xxx...

Has the length attribute

But the length property is not dynamic and will not change with the change of the member

Push (), forEach () and other methods without arrays

ArrayLike.__proto__ = = Object.prototype; / / true arrayLike instanceof Object; / / true arrayLike instanceof Array; / / false

Typical pseudo arrays include the set of DOM elements obtained by $() in jQuery, the arguments object in the function, and the string String object.

Example:

Var arrLike = {0:'a 'arrLike.length;, 1:' baked, 2:'C', length: 3,} arrLike [1]; / /'a 'arrLike.length; / / 3 arrLike.push (' d') / / Uncaught TypeError: the method of converting an arrLike.push is not a function pseudo array into a true array var arrLike = {0: 'axiom, 1:' baked, 2: 'cased, length: 3,}

1. Add an empty array through traversal

Var arr = []; for (var I = 0; I < arrLike.length; iLike +) {arr.push (arrLike [I]);}

It is easy to understand, but the steps are a little tedious.

2. Using the slice () method of the array [recommended]

; [] .slice.call (arrLike)

Or

Array.prototype.slice.apply (arrLike)

Use slice () to return a new array and call () or apply () to point its environment to the pseudo array.

Note that no additional properties other than the index value are retained in this returned array.

For example, the context attribute in the DOM pseudo-array obtained by $() in jQuery will not be retained after it is converted by this method.

Simulate the internal implementation of slice ()

Array.prtotype.slice = function (start, end) {var result = new Array (); var start = start | 0; var end = end | this.length; for (var I = start; I < end; iTunes +) {result.push (This [I]);} return result;}

3. Modify the prototype to point to

ArrLike.__proto__ = Array.prototype

In this way, arrLike inherits the methods in Array.prototype and can use methods such as push (), unshift (), etc., and the duration value will change dynamically.

In addition, this method of directly modifying the prototype chain also retains all the attributes in the pseudo array, including those that are not index values.

4. The Array.from () method in ES2015

The Array.from () method creates a new array instance from a similar array or iterable object.

Var arr = Array.from (arrLike)

The result is similar to the second method, leaving only the attributes within the index value.

Thank you for your reading, these are the contents of "how to understand pseudo arrays in javascript". After the study of this article, I believe you have a deeper understanding of how to understand pseudo arrays in javascript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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