详解 Array.prototype.slice.call(arguments)
首先,slice有两个用法,一个是String.slice,一个是Array.slice,第一个返回的是字符串,第二个返回的是数组
在这里我们看第二个方法
1.在JS里Array是一个类 slice是此类里的一个方法
那么使用此方法应该Array.prototype.slice这么去用
slice从字面上的意思很容易理解就是截取(当然你不是英肓的话)
这方法如何使用呢?
arrayObj.slice(start, [end]) 很显然是截取数组的一部分
2.我们再看call
call([thisObj[,arg1[arg2[[argN]]]]])
thisObj是一个对象的方法
arrg1~argN是参数
那么Array.prototype.slice.call(arguments,1);这句话的意思就是说把调用方法的参数截取出来。
如:
function test(a,b,c,d)
{
var arg = Array.prototype.slice.call(arguments,1);
alert(arg);
}
test("a","b","c","d");
结果是:
这样应该能理解了吧。
3. 然后我再看apply
apply([thisObj[,argArray]])
thisObj:
可选项 将被用作当前对象的对象。
argArray
可选项 将被传递给该函数的参数数组。
4. 来一个实例重写setTimeout 我们知道Timeout不能传参数。(这个可真是不好)
}
window.setTimeout(test,1000,'fason',window,123,[5,6,7],new Object());
5、实际参数在函数中我们可以使用 arguments 对象获得 (注:形参可通过 arguments.callee 获得),虽然 arguments 对象与数组形似,但仍不是真正意义上的数组。
我们可以通过数组的 slice 方法将 arguments 对象转换成真正的数组。
方法一:var args = Array.prototype.slice.call(arguments);
方法二:var args = [].slice.call(arguments, 0);
方法三:
var args = [];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
注:一般的函数的 arguments.length 都在 10 以内,方法二有优势; 方法二的代码量上也比第一种少,至少可以减小一点字节
下面附一个例子:
function revse(){
var args = Array.prototype.slice.call(arguments);
newarr=[];
for(var i=args.length-1;i>=0;i--){
newarr.push(args[i]);
}
return args;
}
var s=revse('a','b','c');
console.log(s);
最后,附个转成数组的通用函数
var toArray = function(s){
try{
return Array.prototype.slice.call(s);
} catch(e){
var arr = [];
for(var i = 0,len = s.length; i < len; i++){
//arr.push(s[i]);
arr[i] = s[i]; //据说这样比push快
}
return arr;
}
}
详解 Array.prototype.slice.call(arguments)的更多相关文章
- Array.prototype.slice.call(arguments)
Array.prototype.slice.call(arguments)能够将具有length属性的对象转化为数组, 可以理解为将arguments转化成一个数组对象,让它具有slice方法 如: ...
- Array.prototype.slice.call(arguments) 类数组转成真正的数组
Array.prototype.slice.call(arguments) 我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数 ...
- 转对象(含length属性)成数组Array.prototype.slice.call(arguments)
我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js ...
- js基础进阶--关于Array.prototype.slice.call(arguments) 的思考
欢迎访问我的个人博客:http://www.xiaolongwu.cn Array.prototype.slice.call(arguments)的作用为:强制转化arguments为数组格式,一般出 ...
- 理解Array.prototype.slice.call(arguments)
在很多时候经常看到Array.prototype.slice.call()方法,比如Array.prototype.slice.call(arguments),下面讲一下其原理: 1.基本讲解 1.在 ...
- Array.prototype.push.apply(a,b)和Array.prototype.slice.call(arguments)
Array.prototype.push.apply(a,b) 时常看到在操作数组的时候有这样的写法: var a = [1,2,3]; var b = [4,5,6]; a.push.apply(a ...
- [转] 理解 JavaScript 中的 Array.prototype.slice.apply(arguments)
假如你是一个 JavaScript 开发者,你可能见到过 Array.prototype.slice.apply(arguments) 这样的用法,然后你会问,这么写是什么意思呢? 这个语法其实不难理 ...
- javascript:Array.prototype.slice.call(arguments)
我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js ...
- [转载]Array.prototype.slice.call(arguments,1)原理
Array.prototype.slice.call(arguments,1)该语句涉及两个知识点. arguments是一个关键字,代表当前参数,在javascript中虽然arguments表面上 ...
随机推荐
- Hibernate常用增删改查方法
/** * @param obj * @return * 添加数据 */ public Serializable saveObject(Object obj){ return this.getHibe ...
- html5响应式设置<meta>
<meta name="viewport" content="width=device-width, initial-scale=1.0"> < ...
- jquery过滤器
<html> <head> <meta charset="UTF-8"> <title>Document</title> ...
- webService 部署以后参数输入框不能显示
在<system.web> 标签下面加入这个 <system.web> <webServices> <protocols> <add name=& ...
- JSP页面以及简单的指令
JSP(Java Server Pages)是指: 在HTML中嵌入Java脚本语言 由应用服务器中的JSP引擎来编译和执行嵌入的Java脚本语言命令 然后将生成的整个页面信息返回给客户端 页 ...
- PowerDesigner V16.5 安装文件 及 破解文件
之前在网上找个假的,只能看,不能创建自己的DB; 或者 不能破解的,比较伤脑筋. 偶在这里提供一个 可长期使用的版本. PowerDesigner165_破解文件.rar 链接:http://p ...
- 通过 --py-files 可以在pyspark中可以顺利导入
文件import问题 问题: 在脚本中import了其他文件, pyspark中可以运行, 但是在spark-submit中总是失败 假定我们的任务脚本为 app.py , 大体代码像这样: from ...
- json对象与json字符串对象格式
var cStr = "{\"c\":\"{\\\"b\\\":\\\"000\\\",\\\"b2\\\&q ...
- Python的时间模块小结(转自:不懂真人)
import datetimeprint time.time() #时间戳 print time.localtime(time.time()) #时间元组 print time.strftime('% ...
- ThinkPHP 3.2.3 关联模型的使用
关于关联模型 ThinkPHP 3.2.3 的关联模型(手册地址)一般处理关联数据表的 CURD 操作,例如关联读取.关联写入.关联删除等. 实例 博客管理模块关于博客有 4 张数据表:博客表 crm ...