English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Analysis of Example Usage of JavaScript Pseudo-Array

This article describes the usage of JavaScript pseudo-arrays. Shared for everyone's reference, as follows:

What is a pseudo-array in JavaScript?

Pseudo-array (array-like): You cannot directly call array methods or expect special behavior from the length property, but you can still iterate over them using real array iteration methods.

1.Typically, the argument parameter of a function,
2.Similar to calling getElementsByTagName, document.childNodes, etc., they all return NodeList objects, which are pseudo-arrays.

So how do you convert a pseudo-array to a standard array?

You can convert the array to a real Array object using Array.prototype.slice.call(fakeArray).

For example, let's use pseudo-arrays to solve the problem of summing arbitrary parameters.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pseudo-Array</title>
</head>
<script>
  function add(){
    var sum=0;
    console.log(arguments);
    for(var i=0;i<arguments.length;i++{
      sum +=arguments[i];
    }
    return sum;
  }
 console.log(add(1,2,5,8));
</script>
<body>
</body>
</html>

Running Result:

Convert the pseudo-array to a standard array

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pseudo-Array</title>
</head>
<script>
  function add(){
    var sum=0;
    console.log(arguments instanceof Array);//It can be judged whether it is a real array at this time, the return value is false;
    console.log(arguments);//The output at this time is the parameters passed in1,2,5,8
    var arguments=Array.prototype.slice.call(arguments);//Convert the pseudo-array to a standard array
    arguments.push(10);//At this time, you can call the standard array methods
    console.log(arguments instanceof Array);//It can be judged whether it is a real array at this time, the return value is true;
    console.log(arguments);//The output at this time is the parameters passed in, the array pushed after1,2,5,8,10
    for(var i=0;i<arguments.length;i++{
      sum +=arguments[i];
    }
    return sum;
  }
 console.log(add(1,2,5,8));
</script>
<body>
</body>
</html>

Running Result:

Readers who are interested in more content related to JavaScript can check out the special topics on this site: 'Summary of JavaScript Data Structures and Algorithm Techniques', 'Summary of JavaScript Mathematical Operation Usage', 'Summary of JavaScript Sorting Algorithms', 'Summary of JavaScript Traversal Algorithms and Techniques', 'Summary of JavaScript Search Algorithm Techniques', and 'Summary of JavaScript Error and Debugging Techniques'.

I hope this article will be helpful to everyone in JavaScript program design.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like