Pseudo-arrays and arrays In JavaScript, except for the five primitive data types, all others are objects, including functions. The relationship between objects and arraysBefore talking about the difference, we need to mention another piece of knowledge, which is JavaScript's prototype inheritance. All JavaScript's built-in constructors inherit from Object.prototype. Under this premise, it can be understood that array objects created using new Array() or [] will have the property values of Object.prototype. var obj = {}; // has the property value of Object.prototype var arr = []; //Array created using array literals, because the properties of Array.prototype are inherited from Object.prototype, //Then, it will have the property values of both Array.prototype and Object.prototype We can see the first difference between objects and arrays: objects do not have the property values of arrays Array.prototype. What is an arrayArrays have a basic feature: index, which objects do not have. Let's look at a piece of code: var obj = {}; var arr = []; obj[2] = 'a'; arr[2] = 'a'; console.log(obj[2]); // => a console.log(arr[2]); // => a console.log(obj.length); // => undefined console.log(arr.length); // => 3
What is a pseudo array?
A pseudo-array is an object that has a length attribute like an array, and also has attributes such as 0, 1, 2, 3, etc. It looks like an array, but it is not an array. For example: var fakeArray = { "0": "first", "1": "second", "2": "third", length: 3 }; for (var i = 0; i < fakeArray.length; i++) { console.log(fakeArray[i]); } Array.prototype.join.call(fakeArray,'+'); Common pseudo-arrays are:
A pseudo-array is an Object, while a real array is an Array. The purpose of pseudo-arrays is to allow ordinary objects to use many methods of arrays normally, such as: var arr = Array.prototype.slice.call(arguments); Array.prototype.forEach.call(arguments, function(v) { // loop over arguments object }); // push //some // every // filter // map // ... The above can be simplified by using array literals when borrowing array prototype methods: var obj = { 0: 'a', 1: 'b', 2: 'c', length: 3 } ;[].push.call(obj, 'd') console.log([].slice.call(obj)) ;[].forEach.call(obj, function (num, index) { console.log(num) }) The difference between the two1. Length:
2. Use of the method:
summary Objects do not have an array property value. The type of the prototype is Object, while the type of the array is Array. SummarizeThis is the end of this article about the usage and differences of JavaScript pseudo-arrays and arrays. For more relevant JavaScript pseudo-array and array content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to add java startup command to tomcat service
>>: Detailed tutorial on installing Nginx 1.16.0 under Linux
Table of contents Preface text 1. Concepts relate...
Anaconda is the most popular python data science ...
MySQL sequence AUTO_INCREMENT detailed explanatio...
1. Basic Specifications (1) InnoDB storage engine...
Business social networking site LinkedIn recently...
Suppose we have n items and we have to sort these...
Preface Sometimes when we view database data, we ...
Table of contents Initialization of echart app-ba...
Let's learn about different types of loops th...
<br />Previous article: Web Design Tutorial ...
Preface When we were writing the web page style a...
It is very easy to delete data and tables in MySQ...
Table of contents Component Design Defining the f...
Table of contents Single Node Diff reconcileSingl...
Table of contents Preface Front-end structure Bac...