how does Array.prototype.slice.call() work?
2014-12-09 03:39
387 查看
http://stackoverflow.com/questions/7056925/how-does-array-prototype-slice-call-work
Q:
I know it is used to make arguments a real array, but I don't understand what happens when using
What happens under the hood is that when
called normally,
an Array, and then it just iterates over that Array, and does its work.
How is
the
an Array? Because when you do:
...the
becomes the value of
the
So with:
...the
is set as the value of
But what if you could substitute something else as the
As long as whatever you substitute has a numeric
and a bunch of properties that are numeric indices, it should work. This type of object is often called an array-like object.
The
let you manually set the value of
a function. So if we set the value of
an array-like object,
just assume it's working with an Array, and will do its thing.
Take this plain object as an example.
This is obviously not an Array, but if you can set it as the
of
then it will just work, because it looks enough like an Array for
work properly.
Example: http://jsfiddle.net/wSvkv/
As you can see in the console, the result is what we expect:
So this is what happens when you set an
as the
of
Because
a
and a bunch of numeric indices,
goes about its work as if it were working on a real Array.
Q:
I know it is used to make arguments a real array, but I don't understand what happens when using
Array.prototype.slice.call(arguments
A:
What happens under the hood is that when
.slice()is
called normally,
thisis
an Array, and then it just iterates over that Array, and does its work.
How is
thisin
the
.slice()function
an Array? Because when you do:
object.method();
...the
objectautomatically
becomes the value of
thisin
the
method().
So with:
[1,2,3].slice()
...the
[1,2,3]Array
is set as the value of
thisin
.slice().
But what if you could substitute something else as the
thisvalue?
As long as whatever you substitute has a numeric
.lengthproperty,
and a bunch of properties that are numeric indices, it should work. This type of object is often called an array-like object.
The
.call()and
.apply()methods
let you manually set the value of
thisin
a function. So if we set the value of
thisin
.slice()to
an array-like object,
.slice()will
just assume it's working with an Array, and will do its thing.
Take this plain object as an example.
var my_object = { '0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four', length: 5 };
This is obviously not an Array, but if you can set it as the
thisvalue
of
.slice(),
then it will just work, because it looks enough like an Array for
.slice()to
work properly.
var sliced = Array.prototype.slice.call( my_object, 3 );
Example: http://jsfiddle.net/wSvkv/
As you can see in the console, the result is what we expect:
['three','four'];
So this is what happens when you set an
argumentsobject
as the
thisvalue
of
.slice().
Because
argumentshas
a
.lengthproperty
and a bunch of numeric indices,
.slice()just
goes about its work as if it were working on a real Array.
相关文章推荐
- [转载]Array.prototype.slice.call(arguments,1)原理
- Array.prototype.slice.call(arguments)
- 对Array.prototype.slice.call(arguments) 的认识
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)简析
- Array.prototype.slice.call
- Array.prototype.slice.call(arguments)
- 转Array.prototype.slice.call(arguments)
- javascript:Array.prototype.slice.call(arguments)
- 浅谈javascript的Array.prototype.slice.call
- [Javascript] Array.prototype.slice.call(obj)用法
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments,1)原理
- javascript的Array.prototype.slice.call
- Array.prototype.slice.call
- Array.prototype.slice.call(arguments)
- JS 有趣的应用 :Array.prototype.slice.call(arguments,0)
- Array.prototype.slice.call(arguments) (转)
- arguments和Array.prototype.slice.call(arguments,0);
- 【笔记】js Array.prototype.slice.call(arguments) 将函数的参数转换为数组方法的见解