- MooTools 1.3 Cookbook
- Jay Larry G. Johnston
- 388字
- 2021-04-02 19:07:05
Storing a list of names in an array of values
In this recipe we will learn how to use a standard data structure called an Array to store a list of names or values.
Getting ready
To create an array, a storage element that holds a list of values, elements, or objects, we use the raw JavaScript Array object to define a literal array var myarray = [1, 2, 'my 3rd value']
;. In our example, we first declare our array; it is in an empty state, then we call upon either raw JavaScript's push()
array object method or MooTools' extension of the array native include()
, based on the ternary output, to add our string value to the array.
How to do it...
Add items to an array. Allow or disallow the addition of duplicate items with a switch in the form.
<script type="text/javascript" src="mootools-1.3.0.js"></script> </head> <body> <form action="" method="get"> <input type="text" id="myitem"/> Ignore if already in the array? <input type="checkbox" id="unique"/> <input type="button" id="mybutton" value="Add This" onclick="store_item_in_array();"/> </form> <script type="text/javascript"> // declare the array var myarray = []; // an array-dedicated utility function to add elements function store_item_in_array() { // use the $ object to get element with id "myitem" var myitem = $('myitem').value; // ternary operators can save a lot of space var ischecked = $('unique').get('checked'); var unique = (ischecked) ? 1 : 0; if (!unique) { // (A) add an item to an array with raw JavaScript myarray.push(myitem); } else { // (B) add an item to an array, but make it moonique myarray.include(myitem); } alert('Length of Array: '+myarray.length); } </script>
How it works...
Much like $, arrays are infused with methods that extend their capability. This snippet calls the JavaScript inherent push()
, which post-pends the single argument to the array, like this: var myarray.push('myvalue')
;.
Note
NOTE: The JavaScript array object will hold much more than string values: it can hold integers, objects, even other array objects to create multi-dimensional arrays!
MooTools has further extended the array prototype by adding new, useful methods. The method used here is called include()
, which works identically both in syntax and in function to push()
; however, it adds a duplicate value check to the incoming argument. If one or more values present are matched, the function does not add a value to the array.
There's more
We should open up the uncompressed version of our MooTools and search for the phrase "include". We can quickly see how include()
is an abstraction that enhances push()
.
- Moodle 1.9 for Teaching 7/14 Year Olds: Beginner's Guide
- Photoshop CC實戰從入門到精通
- 青少年美育趣味課堂:青少年學攝影修圖
- NHibernate 2 Beginner's Guide
- Premiere Pro影視后期編輯:短視頻制作實戰寶典
- Photoshop CC移動UI設計實用教程
- Photoshop CC摳圖+修圖+調色+合成+特效實戰視頻教程
- Adobe創意大學Photoshop產品專家認證標準教材(CS6修訂版)
- 中文版3ds Max 2022基礎教程
- Photoshop海報設計技巧與實戰
- Instant Apache Sqoop
- Excel數據處理與分析:數據思維+分析方法+場景應用
- 平面設計師實戰教程
- Photoshop CC中文版基礎教程
- 計算機圖形學編程(使用OpenGL和C++)