prototype中的$A函数的用法

简介:

 
 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2.  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html> 
  4. <head> 
  5.     <title>$A</title> 
  6.     <script language="javascript" src="prototype.js" 
  7.      type="text/javascript"></script> 
  8.     <script language="javascript" type="text/javascript"> 
  9.     function showOptions() { 
  10.         //先获取id为listFramework的对象,再由对象找到tagName为option的内容 
  11.         var someNodeList = $("lstFramework").getElementsByTagName("option"); 
  12.         var nodes = $A(someNodeList); 
  13.          
  14.         var info = []; 
  15.          
  16.         nodes.each (function(node){ 
  17.             info.push(node.value + ": " + node.innerHTML); 
  18.         }); 
  19.          
  20.         alert(info.join("\r\n")); 
  21.     } 
  22.     </script> 
  23. </head> 
  24. <body> 
  25.     <form> 
  26.         <select id="lstFramework" size="10"> 
  27.             <option value="1">Prototype</option> 
  28.             <option value="2">Script.aculo.us</option> 
  29.             <option value="3">Dojo</option> 
  30.             <option value="4">YUI</option> 
  31.         </select> 
  32.         <input onclick="showOptions();" type="button" value="Show the options"> 
  33.     </form> 
  34. </body> 
  35. </html> 

看看官方的解释你就都明白了:

Accepts an array-like collection (anything with numeric indices) and returns its equivalent as an actual Array object. This method is a convenience alias of Array.from, but is the preferred way of casting to an Array.

The primary use of $A is to obtain an actual Array object based on anything that could pass as an array (e.g. the NodeList or HTMLCollection objects returned by numerous DOM methods, or the predefined arguments reference within your functions).

The reason you would want an actual Array is simple: Prototype extends Array to equip it with numerous extra methods, and also mixes in the Enumerable module, which brings in another boatload of nifty methods. Therefore, in Prototype, actual Arrays trump any other collection type you might otherwise get.

The conversion performed is rather simple: nullundefined and false become an empty array; any object featuring an explicit toArray method (as many Prototype objects do) has it invoked; otherwise, we assume the argument "looks like an array" (e.g. features a length property and the [] operator), and iterate over its components in the usual way.

When passed an array, $A makes a copy of that array and returns it.


本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/410388,如需转载请自行联系原作者


相关文章
|
1天前
|
JavaScript 前端开发
在JavaScript中,函数原型(Function Prototype)是一个特殊的对象
【5月更文挑战第11天】JavaScript中的函数原型是一个特殊对象,它为所有函数实例提供共享的方法和属性。每个函数在创建时都有一个`prototype`属性,指向原型对象。利用原型,我们可以向所有实例添加方法和属性,实现继承。例如,我们定义一个`Person`函数,向其原型添加`greet`方法,然后创建实例`john`和`jane`,它们都能调用这个方法。尽管可以直接在原型上添加方法,但推荐在构造函数内部定义以封装数据和逻辑。
18 2
|
1天前
|
Java 编译器 Linux
【C++11(二)】lambda表达式以及function包装器
【C++11(二)】lambda表达式以及function包装器
|
JavaScript 前端开发
|
JavaScript 前端开发
Function() 构造函数
Function() 构造函数
47 0
Array.prototype.includes() 原型调用用法案例讲解
Array.prototype.includes() 原型调用用法案例讲解
89 2
重写Function.prototype.bind
重写Function.prototype.bind
94 0
|
JSON JavaScript 前端开发
学习javaScript必知必会(6)~类、类的定义、prototype 原型、json对象
学习javaScript必知必会(6)~类、类的定义、prototype 原型、json对象
127 0
学习javaScript必知必会(6)~类、类的定义、prototype 原型、json对象
|
JavaScript 前端开发
函数原型中的 call 和 apply 方法的区别
它们是在 JavaScript 引擎内在实现的,因为属于Function.prototype,所以每个Function对象实例,也就是每个方法都有call, apply属性。它们的作用一样,只是使用方式不同。
111 0
|
Web App开发 JavaScript 前端开发