javascript jquery中toArray和makeArray的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16434145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between toArray and makeArray in jquery
提问by user2073289
I'm trying to convert the DOM element as an collections of object. But I don't know what the main difference between toArray() and makeArray()
我正在尝试将 DOM 元素转换为对象的集合。但我不知道 toArray() 和 makeArray() 之间的主要区别是什么
HTML
HTML
<div id="firstdiv">
<div>foo1</div>
<div>foo2</div>
<div>foo3</div>
</div>
I used the following code to convert the nodes to an array:
我使用以下代码将节点转换为数组:
JQUERY
查询
console.log($("#firstdiv > div").toArray());
console.log($.makeArray($("#firstdiv").html()));
I can't quite understand the difference between them, and I've searched for this question but not found any clear explanation.
我不太明白它们之间的区别,我已经搜索了这个问题,但没有找到任何明确的解释。
Thanks in advance.
提前致谢。
回答by Ale? Kotnik
According to jQuery documentation:
根据 jQuery 文档:
toArray
is a method on jQuery Object (which is wrapper around a set of DOM elements). This method extracts members of this set of DOM elements to javascript Array:
toArray
是 jQuery 对象上的一个方法(它是一组 DOM 元素的包装器)。此方法将这组 DOM 元素的成员提取到 javascript Array 中:
jQuery('.some-class').toArray() -> [ dom_el_1, dom_el_2, dom_el_3, ... ]
makeArray
(which is a "static method" on jQuery object) takes array-like object(jQuery, arguments, nodeList, ...) and constructs a proper javascript Array from it, so you can call methods of Array on the result:
makeArray
(这是 jQuery 对象上的“静态方法”)采用类数组对象(jQuery、参数、nodeList 等)并从中构造一个适当的 javascript 数组,因此您可以对结果调用 Array 的方法:
// returns a nodeList (which is array like item) but not actual array
// you can't call reverse on int
var elems = document.getElementsByTagName("div");
var arr = jQuery.makeArray(elems);
arr.reverse(); // use an Array method on list of dom elements
$(arr).appendTo(document.body);
In summary, toArray
transforms jQuery element setto javascript Array
, makeArray
transforms any array like objectto javascript Array
.
总之,toArray
将jQuery 元素设置为 javascript Array
,makeArray
将任何数组(如 object )转换为 javascript Array
。
回答by Zaheer Ahmed
The only difference is:
唯一的区别是:
toArray()
is DOM Element Methods
and you can only used it on dom elements. while makeArray(object)
can be used on your custom objects.
toArray()
isDOM Element Methods
并且您只能在 dom 元素上使用它。whilemakeArray(object)
可用于您的自定义对象。
There is no other differences they are mostly same and return plain array object.
没有其他区别,它们大多相同并返回普通数组对象。
Example
例子
You have any custom object:
您有任何自定义对象:
function Person(name,age) {
this.name = name;
this.age = age;
}
var obj=new Person('abc',1);
now to use both function:
现在使用这两个功能:
jQuery.makeArray(obj).length; //would return 1
while:
尽管:
obj.toArray().length; //through the error function not defined
and
和
obj.length; //return undefined
回答by Steven Pribilinskiy
As it was mentioned already toArray()
is for jQuery objects.
正如已经提到的,它toArray()
是针对 jQuery 对象的。
$.makeArray()
is similar to JS Array.prototype.slice.call()
or [].slice.call()
for short, though the latter instantiates an object with []
$.makeArray()
类似于 JSArray.prototype.slice.call()
或[].slice.call()
简称,虽然后者实例化一个对象[]
But there's a difference when processing POJOs with excessive length
value
但是在处理length
值过大的 POJO 时是有区别的
Examples
例子
1.
1.
$.makeArray({ 0:'a', 1:'b', length:1 })
// ["a"]
[].slice.call({ 0:'a', 1:'b', length:1 })
// ["a"]
2.
2.
$.makeArray({ 0:'a', 1:'b', length:4 })
// { 0:'a', 1:'b', length:4 } // WUT?
[].slice.call({0:'a', 1:'b', length:4})
// ["a", "b", undefined, undefined]
3.
3.
$.makeArray({12:'a', 13:'b', length:1})
// { 12:'a', 13:'b', length:1 } // WUT?
[].slice.call({12:'a', 13:'b', length:1})
// [undefined]
4.
4.
$.makeArray({ 0:'a', 2:'b', length:2 })
// { 0:'a', 2:'b', length:2 } // WUT?
[].slice.call({ 0:'a', 2:'b', length:2 })
// ["a", undefined]
Thus $.makeArray()
simply returns the input object whenever a key with particular index is not found.
因此,$.makeArray()
只要没有找到具有特定索引的键,就简单地返回输入对象。
It will work fine only when converting array-like objects with proper number of items in the array e.g. function's arguments
, nodeList
, jQuery collections, etc.
只有在转换数组中具有适当数量的项目的类数组对象(例如函数的arguments
、nodeList
、 jQuery 集合等)时,它才能正常工作。