Prototype.js - SPLessons

Prototype.js Arrays

Home > Lesson > Chapter 6
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Prototype.js Arrays

Prototype.js Arrays

shape Introduction

This chapter demonstrates about the Prototype.js Arrays which expands all native JavaScript arrays with some powerful methods and following are the concepts are covered in this chapter.
  • Array and Array Methods

Array and Array Methods

shape Description

Prototype expands all native JavaScript arrays with some powerful methods which can be done in two ways those are shown below.
  • Which mixes in the Enumerable module and it brings in a ton of methods.
  • Which adds some few extra methods these are documented in present section only.
Prototype doesn't have any way to mark the methods which adds to Array.prototype as non-enumerable by using for...in on arrays while using prototype it will enumerate the all extended methods those are coming from the enumerable module and those are puts in the array namespace. The snippet below demonstrates the writing of for loop. [jscript] for (var index = 0; index < myArray.length; ++index) { var item = myArray[index]; } [/jscript] Now user can replace the above code with prototype as shown in below snippet. [jscript] myArray.each(function(item) { }); [/jscript] Following are the some Array methods briefly explained as shown below. from Which method clones an existing array or creates a new one from an array like collection. as shown in the below code. [jscript] <html> <head> <title>Prototype examples</title> <script type="text/javascript" src="/javascript/prototype.js"></script> <script> function showOptions() { var NodeList = $('employees').getElementsByTagName('option'); var nodes = Array.from(NodeList); nodes.each(function(node) { alert(node.nodeName + ': ' + node.innerHTML); }); } </script> </head> <body> <select id = "employees" size = "10" > <option value = "5">John, Kate</option> <option value = "8">Debi, Jack</option> <option value = "1">Dany, Nelson</option> </select> <br /> <input type = "button" value = "Show the options" onclick = "showOptions();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image. clear Which method is used to clear an array is shown in below snippet. [jscript] <html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { var guys = ['Sam', 'Justin', 'Andrew', 'Dan']; alert("Before using clear" ); guys.each(function(item) { alert(item ); }); guys.clear(); alert("After using clear" ); guys.each(function(item) { alert(item ); }); } </script> </head> <body> <p>Click the button to get the result.</p> <br /> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image. When user click on the result button a box will appear as shown in below image. clone Clone returns a duplicate of the array, leaving the original array intact is as shown in below snippet. [jscript] <html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { var oldArr = ['Sam', 'Justin', 'Andrew', 'Dan']; var newArr = oldArr.clone(); alert("Guys in old Arr" ); oldArr.each(function(item) { alert(item ); }); alert("Guys in new Arr" ); newArr.each(function(item) { alert(item ); }); } </script> </head> <body> <p>Click the button to see the result.</p> <br /> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image. first Which method returns the first item of an array if the array empty is as shown in below snippet. [jscript] <html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { var arr = ['Sam', 'Justin', 'Andrew', 'Dan']; alert("First element is : " + arr.first() ); } </script> </head> <body> <p>Click the button to see the result.</p> <br /> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image. last Which method returns the last item of an array or undefined if  array is empty is as shown in below image. [jscript] <html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { var arr = ['Apples', {good: 'yes', bad: 'no'}, 3, 34]; alert("Last Element : " + arr.last() ); } </script> </head> <body> <p>Click the button to see the result.</p> <br /> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image.

Summary

shape Key Points

  • Clear will clear an entire array.
  • Prototype expands all native java script arrays.
  • Clone returns duplicate of the array.