Yan*_*sky 185 javascript arrays call slice
我偶然发现了将DOM NodeList转换为常规数组的简洁快捷方式,但我必须承认,我并不完全理解它是如何工作的:
[].slice.call(document.querySelectorAll('a'), 0)
Run Code Online (Sandbox Code Playgroud)
所以它从一个空数组开始[]
,然后slice
用于将结果转换call
为一个新数组是啊?
我不明白的是call
.如何document.querySelectorAll('a')
从NodeList转换为常规数组?
Max*_*keh 148
这里发生的事情就是你称之为使用slice()
的功能.什么做在这种情况下创建一个空数组,然后通过运行它的对象(最初的数组,现在迭代),并保留追加该对象的元素,它创建的空数组,这是最终返回.这是一篇关于此的文章.NodeList
call()
slice()
NodeList
编辑:
所以它以空数组[]开头,然后使用slice将调用结果转换为新数组是啊?
那是不对的.[].slice
返回一个函数对象.函数对象具有call()
调用指定call()
to 的第一个参数的函数的函数this
; 换句话说,使函数认为它是从参数(NodeList
返回的document.querySelectorAll('a')
)而不是从数组中调用的.
sle*_*man 114
在javascript中,对象的方法可以在运行时绑定到另一个对象.简而言之,javascript允许对象"借用"另一个对象的方法:
object1 = {
name:'frank',
greet:function(){
alert('hello '+this.name)
}
};
object2 = {
name:'andy'
};
// Note that object2 has no greet method.
// But we may "borrow" from object1:
object1.greet.call(object2); // will show an alert with 'hello andy'
Run Code Online (Sandbox Code Playgroud)
在call
和apply
函数对象(在JavaScript函数是对象也一样)的方法,可以让你做到这一点.因此,在您的代码中,您可以说Nodelist正在借用数组的切片方法.转换是什么,slice返回另一个数组,因为它的结果.
Raj*_*amy 12
如何
document.querySelectorAll('a')
从a 转换NodeList
为常规数组?
这是我们的代码,
[].slice.call(document.querySelectorAll('a'), 0)
Run Code Online (Sandbox Code Playgroud)
让我们先拆掉它,
[] // Array object
.slice // Accessing the function 'slice' present in the prototype of Array
.call // Accessing the function 'call' present in the prototype of function object(slice)
(document.querySelectorAll('a'),0)
// 'call' can have arguments like, (thisArg, arg1,arg2...n).
// So here we are passing the 'thisArg' as an array like object,
// that is a 'nodeList'. It will be served as 'this' object inside of slice function.
// And finally setting 'start' argument of slice as '0' and leaving the 'end'
// argument as 'undefined'
Run Code Online (Sandbox Code Playgroud)
步骤:1执行call
功能
call
,比其他thisArg
的参数的其余部分将被追加到的参数列表.slice
通过将其this
值
thisArg
(像对象来自的数组document.querySelector
)和参数列表绑定来调用该函数.ie] start
包含的参数0
步骤:2执行slice
内部调用的函数call
start
将被分配给变量s
as0
end
是undefined
,this.length
将被存储e
a
完成上述设置后,将发生以下迭代
while(s < e) {
a.push(this[s]);
s++;
}
Run Code Online (Sandbox Code Playgroud)a
将作为结果返回.PS为了更好地理解我们的场景,我们的上下文所需的一些步骤已经从原始的call和slice算法中忽略了.
[].slice.call(document.querySelectorAll('.slide'));
1. The querySelectorAll() method returns all elements in the document that matches a specified selector(s).
2. The call() method calls a function with a given this value and arguments provided individually.
3. The slice() method returns the selected elements in an array, as a new array object.
so this line return the array of [object HTMLDivElement]. Here is the six div with classname "slide" so array length will be 6.
<div class="slideshow">
<div class="slide">
first slider1
</div>
<div class="slide">
first slider2
</div>
<div class="slide">
first slider3
</div>
<div class="slide">
first slider4
</div>
<div class="slide">
first slider5
</div>
<div class="slide">
first slider6
</div>
</div>
<script type="text/javascript">
var arraylist = [].slice.call(document.querySelectorAll('.slide'));
alert(arraylist);
</script>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
68016 次 |
最近记录: |