js基础进阶--关于Array.prototype.slice.call(arguments) 的思考
欢迎访问我的个人博客:http://www.xiaolongwu.cn
Array.prototype.slice.call(arguments)的作用为:强制转化arguments为数组格式,一般出现在框架活插件的源码中
如何理解
上面的代码等价于[ ].slice.call(arguments)
或者随便一个数组调用都行 [1,2,4].slice.call(arguments)
因为,前面的调用者的作用只是沿着原型链向上找,最终找到Array为止,slice为Array原型上的一个方法
slice内部的实现原理大概是这样的
Array.prototype.slice = function(start,end){
var result = new Array();
start = start || 0; // 如果不传则取默认值
end = end || this.length; // 如果不传则取默认值
//this指向调用的对象,当用了call后,能够改变this的指向,也就是指向传进来的对象,这是关键
for(var i = start; i < end; i++){
result.push(this[i]);
}
return result;
}
数组和字符串都有slice方法
如果对slice不是很了解,请看这篇文章:slice()与splice()的用法和区别你清楚吗?
call方法的作用为:改变调用者的this指向并且执行调用者,
function fn(a){
console.log(this.a);
console.log(this);
}
var obj = {
a : 2
}
fn.call(obj); // 2
// {a : 2}
// 解释一下 fn的this指针指向了obj ,并且执行了fn函数
如果对call方法还是没理解,请看这边文章 js基础--深入理解call、apply、bind
arguments 为函数实参的一个集合,数据类型为对象类型
写一个例子吧
function ary(){
console.log(arguments,typeof arguments,arguments instanceof Object)
// 自己实操一下 看看上面这行代码能打印出什么
return [11,12].slice.call(arguments);
//这里的[11,12]可以被替换为任意的数组,不影响结果
}
var a11 = ary(1,2,3,4,5,6,888,9,222);
console.log(a11); [1, 2, 3, 4, 5, 6, 888, 9, 222]
console.log(typeof a11); //"object"
console.log(a11 instanceof Array); //ture
技术延伸
其实要实现将arguments强制转化为数组,还有几种方式
利用es6的Array.from()
function fn(){
var a = Array.from(arguments);
var b = Array.from(arguments).slice(1);
console.log(a);
console.log(b);
}
fn(1,2,6,3,4,12);
// 结果分别为 1,2,6,3,4,12 和 2,6,3,4,12
利用es6的展开表达式
function fn(){
var a = [...arguments];
var b = [...arguments].slice(1);
console.log(a);
console.log(b);
}
fn(1,2,6,3,4,12);
// 结果分别为 1,2,6,3,4,12 和 2,6,3,4,12
本文完
github资源地址:https://github.com/关于Array.prototype.slice.call(arguments) 的思考.md
我的个人博客:http://www.xiaolongwu.cn
csdn博客地址:https://blog.csdn.net/wxl1555
如果您对我的博客内容有疑惑或质疑的地方,请在下方评论区留言,或邮件给我,共同学习进步。
邮箱:wuxiaolong802@163.com
js基础进阶--关于Array.prototype.slice.call(arguments) 的思考的更多相关文章
- js Array.prototype.slice.call(arguments,0) 理解
Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数 网上很多复制粘帖说:Array.prototype.slice.call(argu ...
- 详解 Array.prototype.slice.call(arguments)
首先,slice有两个用法,一个是String.slice,一个是Array.slice,第一个返回的是字符串,第二个返回的是数组 在这里我们看第二个方法 1.在JS里Array是一个类 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 ...
- 理解Array.prototype.slice.call(arguments)
在很多时候经常看到Array.prototype.slice.call()方法,比如Array.prototype.slice.call(arguments),下面讲一下其原理: 1.基本讲解 1.在 ...
- javascript:Array.prototype.slice.call(arguments)
我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js ...
- 解析 Array.prototype.slice.call(arguments,0)
Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数 网上很多复制粘帖说:Array.prototype.slice.call(argu ...
- Array.prototype.slice.call(arguments)
Array.prototype.slice.call(arguments)能够将具有length属性的对象转化为数组, 可以理解为将arguments转化成一个数组对象,让它具有slice方法 如: ...
- 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 ...
随机推荐
- Linux学习笔记 --服务器优化
Linux服务器优化 序言: 服务器操作建议 1.严格按照目录规范操作服务器 2.远程服务器不允许关机 3.不要在服务器访问高峰运行高负载命令 4.远程配置防火墙时,不要把自己踢出服务器 一.禁用不必 ...
- 知名IT公司的年度大会合集
很多知名的IT公司都有年度大会,比如说谷歌,微软,Adobe,甲骨文,苹果等等.在这些公司的年度大会上,都会展示一些公司比较前沿的产品.看看这些大会的视频(也可以参会,但是门票可是非常贵的),对我们了 ...
- ITU-T G.1080 IPTV的体验质量(QoE)要求 (Quality of experience requirements for IPTV services)
IPTV的服务质量(QoE)要求 Quality of experience requirements for IPTV services Summary This Recommendation de ...
- C语言获取系统时间的函数
在标准C编程中,我们如何写程序来获取当前系统的时间呢? 获取系统时间的函数 #include <time.h> time_t time(time_t *t) 参数:如果参数不为空,那么 ...
- Hbase问题
Q: .meta.和root表是否要分裂? A: meta表和root表不会分裂,代码中有所判断. Q: 如果不分裂,那么都只有1个region? A: ... (查看代码后)A: meta和root ...
- Xcode and #pragma mark
原帖地址:http://macdevelopertips.com/xcode/xcode-and-pragma-mark.html I've started using #pragma mark di ...
- 超高速前端开发工具——Emmet
[由于 CSDN 不支持富文本编辑器写的文章迁移到 Markdown 编辑器中修改,已重发了一个重新排版的版本, 新版链接:http://blog.csdn.net/ys743276112/artic ...
- os X下mds_stores占用大量cpu的解决办法
有时候发现, MacOS中, 有个叫做mds_stores的进程占了好多CPU, 于是要阻止这个行为, 据说这是MacOS在建索引然后只要把这个索引的关掉就好了 sh-3.2# mdutil -a - ...
- ORM框架--GreenDao 3.0基本使用指南
0. ORM框架--GreenDao 3.0基本使用指南 1. Gradle 的配置 这里可以参照官方的文档进行最新的配置(本示例的版本等你看到可能就不是最新的了),但是值得注意的一点是,GreenD ...
- sqlServer遇到的问题
重置自增列:dbcc checkident(表名,reseed,数字(初始值))