理解Array.prototype.slice.call(arguments)是如何工作的?
我直接上例子,我就不翻译了,真是怕翻译错了。
var a={length:2,0:'first',1:'second'};//类数组,有length属性,长度为2,第0个是first,第1个是second console.log(Array.prototype.slice.call(a,0));// ["first", "second"],调用数组的slice(0); var a={length:2,0:'first',1:'second'}; console.log(Array.prototype.slice.call(a,1));//["second"],调用数组的slice(1); var a={0:'first',1:'second'};//去掉length属性,返回一个空数组 console.log(Array.prototype.slice.call(a,0));//[] function test(){ console.log(Array.prototype.slice.call(arguments,0));//["a", "b", "c"],slice(0) console.log(Array.prototype.slice.call(arguments,1));//["b", "c"],slice(1) } test("a","b","c");
arguments本身不是一个数组,可以说是有length属性的一个对象(类数组对象),所以需要将其变通,改造成一个数组。
JS数组用法,参考此链接
stackoverflow:原帖
What happens under the hood is that when .slice()
is called normally, this
is an Array, and then it just iterates the Array, and does its work.
How is this
in the .slice()
function an Array? Because when you do:
object.method();
...the object
automatically becomes the value of this
in the method()
. So with:
[1,2,3].slice()
...the [1,2,3]
Array is set as the value of this
in .slice()
.
But what if you could substitute something else as the this
value? As long as whatever you substitute has a numeric .length
property, 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 this
in a function. So if we set the value of this
in .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 this
value 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 arguments
object is the this
value of .slice()
. Because arguments
has a .length
property and a bunch of numeric indices, .slice()
just goes about its work as though it was working with an Array.
相关推荐
永远不要写Array.prototype.slice.call(arguments); 以后再! 这是基于但使用Object.defineProperty(arguments.constructor.prototype, [functionName], {enumerable: false, configurable: true, value: [function...
在js中我们经常会看到Array.prototype.slice.call(arguments,0)的写法,当然,这个方法的作用也许大家都明白,那就是把类数组对象转换成一个真正的数组。关于这个方法,我说说自己的理解。 这里涉及到slice()方法和...
arguments在JavaScript语法中是函数特有的一个对象属性(Arguments对象),用来引用调用该函数时传递的实际参数。
经常的,可以看到Array.prototype.slice(arguments, 0); 这个写法可以用于function() {} 内,这样可以将函数的参数列表转换成一个真正的数组。请看一个例子: 代码如下:var slice = Array.prototype.slice;var ...
在JavaScript中,数组可以使用Array构造函数来创建,或使用[]快速创建,这也是首选的方法。数组是继承自Object的原型,... var nodesArr = Array.prototype.slice.call(document.forms); var argsArr = Array.pro
代码如下: [removed] function fn(name){ if(typeof name === “string”){ var args = Array.prototype.slice.call( arguments, 1 ); for(var i=0;i<args.length;i++){ alert(args[i]);//结果: 111 222 ...
这篇文章来回答javascript通用循环遍历方法forEach中最后提到的关于伪数组的... 我们可以通过Array.prototype.slice.call(fakeArray)将伪数组转变为真正的Array对象。 来看个示例: 代码如下: var fakeArray01 = {0:’a
高级JavaScript 本文档基于John Resig的高级JavaScript网站。 如果这些概念对您而言似乎很复杂,那么我建议您使用,同时也强烈建议您使用。 如果您认为自己了解大多数这些概念,则... call ( arguments ) , object =
arguments定义 所有的函数都有一个自己的arguments对象,用来储存它实际接受到的参数,...var args = Array.prototype.slice.call(arguments); 类数组 1. 判断ARGUMENTS是不是数组 alert(arguments instanceof Arra
虽然现在慢慢减少了对jQuery的使用(项目上还是用,效率高点。平时基本不用了),希望从而减少对jQuery的依赖度。 但是这链式操作的方式实在吸引人(貌似现在... //var args = Array.prototype.slice.call(arguments,0
虽然现在慢慢减少了对jQuery的使用(项目上还是用,效率高点。平时基本不用了),希望从而减少对jQuery的依赖度。 但是这链式操作的方式实在吸引人(貌似现在... //var args = Array.prototype.slice.call(arguments,0
昨天阿丹传了一个javascript中的重载例子给我,感觉不错.... window.setTimeout = function(fRef, mDelay) { if(typeof fRef == ‘function’){ var argu = Array.prototype.slice.call(arguments,2); va
昂尼克斯rpc 和 subpub安装 $ ... call ( arguments ) . pop ( ) ; cb ( null , a * b ) ;} ) ;onnexA . publish ( "time tick" ) ;setInterval ( function ( ) { onnexA . publish ( "time tick" , new Date ( ) . g
本文实例讲述了js prototype深入理解及应用。分享给大家供大家参考,具体... var args = Array.prototype.slice.call(arguments, 1); return args; } 咦???看着上面这行代码,你是不是对prototype只是属于函数产生
值得庆幸的是,我们可以通过数组的 slice 方法将 arguments 对象转换成真正的数组: var args = Array.prototype.slice.call(arguments);对于slice 方法,ECMAScript 262 中 15.4.4.10 Array.prototype.slice (start...
本文实例分析了JS中setTimeout()无法调用带参函数问题的解决方法。分享给大家供大家参考,具体如下: ... var argu = Array.prototype.slice.call(arguments, 2); var f = function(){ fRef.apply(null, arg
对于 arguments,可以使用 Array.prototype.slice.call(arguments); 来达到转换的目的,但对于 NodeList 则不行了,其在 IE8 及以下将会报错,只能说其 JS 引擎有所限制。 因此,如果需要把 NodeList 转换为真正的...
call ( arguments , 0 ) ; return exports . normalize ( paths . filter ( function ( p , index ) { if ( typeof p !== 'string' ) { throw new TypeError ( 'Arguments to path.join must be strings' ) ;
default:s=Array.prototype.slice.call(arguments,1),r.apply(this,s)}else if(n(r))for(s=Array.prototype.slice.call(arguments,1),u=r.slice(),a=u.length,l=0;l;l++)u[l].apply(this,s);return!0},r.prototype....
帕斯卡 js中的Pascal解析器。 我们的目标是能够运行90年代依赖于asm的简单Turbo Pascal演示,但否则会非常简单。 生成的js输出非常明智... call ( arguments ) ; console . log ( args . join ( '' ) ) ; }var x = nul