【转】

前言

 今天偶然翻资料看到一个叫做软绑定的函数,用来确定this的;

原代码

  if(!Function.prototype.softBind){
Function.prototype.softBind = function(obj){
var fn = this;
var curried = [].slice.call(arguments,1);
var bound = function(){
return fn.apply(
(!this || this === (window || global)) ? obj:this,
curried.concat.apply(curried,arguments);
)
};
bound.prototype = Object.create(fn.prototype);
return bound;
}
}

看到[].slice.call(arguments,1) 这个写法我一脸懵逼,为何?

arguments是一个对象而不是数组..而且自身的原型链上也没有slice这个方法;


个人见解

  • []自身也是也是一个对象.而数组原型链上有这个slice这个方法,通过call显式绑定来实现arguments变相有slice这个方法

     /*此处的返回值是true*/
    [].slice === Array.prototype.slice; /*确定arguments的类型
    * 返回 3,Object, true;
    */
    (function(a,b,c){
    console.log(arguments.length);
    console.log(typeof arguments);
    console.log( arguments instanceof Object); }(1,2,3)) /*我们自身也可以模拟一个对象传入,我们这里用数组对象,不是等同,只是道理差不多的*/
    aargument = [1,2,3,4];
    [].slice.call(aargument,3); //返回[4]
    [].slice.call(aargument,1,3); //[2, 3]

    总结

    那个写法就是裁切arguments传入的参数,保存起来操作;

[].slice.call(arguments,1)的更多相关文章

  1. Array.prototype.slice.call(arguments)

    Array.prototype.slice.call(arguments)能够将具有length属性的对象转化为数组, 可以理解为将arguments转化成一个数组对象,让它具有slice方法 如: ...

  2. 详解 Array.prototype.slice.call(arguments)

    首先,slice有两个用法,一个是String.slice,一个是Array.slice,第一个返回的是字符串,第二个返回的是数组 在这里我们看第二个方法 1.在JS里Array是一个类 slice是 ...

  3. Array.prototype.slice.call(arguments) 类数组转成真正的数组

    Array.prototype.slice.call(arguments)   我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数 ...

  4. 转对象(含length属性)成数组Array.prototype.slice.call(arguments)

    我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js ...

  5. js基础进阶--关于Array.prototype.slice.call(arguments) 的思考

    欢迎访问我的个人博客:http://www.xiaolongwu.cn Array.prototype.slice.call(arguments)的作用为:强制转化arguments为数组格式,一般出 ...

  6. JS之函数实际参数转换成数组的方法[].slice.call(arguments)

    实际参数在函数中我们可以使用 arguments 对象获得 (注:形参可通过 arguments.callee 获得),虽然 arguments 对象与数组形似,但仍不是真正意义上的数组. 我们可以通 ...

  7. 理解Array.prototype.slice.call(arguments)

    在很多时候经常看到Array.prototype.slice.call()方法,比如Array.prototype.slice.call(arguments),下面讲一下其原理: 1.基本讲解 1.在 ...

  8. 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 ...

  9. [转] 理解 JavaScript 中的 Array.prototype.slice.apply(arguments)

    假如你是一个 JavaScript 开发者,你可能见到过 Array.prototype.slice.apply(arguments) 这样的用法,然后你会问,这么写是什么意思呢? 这个语法其实不难理 ...

随机推荐

  1. Pseudo-devices On GNU/Linux

    先分享一则有意思Q&A,来自The FreeBSD Funnies 17.4 . Where does data written to* /dev/null* go? ​ It goes in ...

  2. Bandit Wargame Level12 Writeup

    Level Goal The password for the next level is stored in the file data.txt, which is a hexdump of a f ...

  3. redis的hash类型

    1.简单描述 hash是一个string类型的field和value的映射表.添加和删除操作都是O(1)(平均)的复杂度.hash类型特别适合用于存储对象.在field的数量在限制的范围内以及valu ...

  4. 添加用户useradd,给用户设置修改密码passwd,修改用户信息usermod,修改用户密码状态chage,删除用户userdel,查询用户及组id,切换用户su,查看当前环境变量env

    useradd 用户名 passwd 用户名,给指定用户设密码 passwd给当前用户设密码 添加一个用户系统会自动在以下文件或目录创建对应用户信息: [root@localhost ~]# grep ...

  5. AutoLayout的几种方法

    1.XIB 2.Fram 3.屏幕比例适配(个人比较推荐)  iOS屏幕适配(尺寸适配) 4.NSLayoutConstraint. 5.Masonry  概述 使用 Objective-C 纯代码编 ...

  6. 通过js中的useragrent来判断设备是pc端还是移动端,跳转不同的地址

    if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alc ...

  7. hiberation4 获取session

    T t; Configuration cfg = new Configuration(); cfg.configure(); ServiceRegistry serviceRegistry = new ...

  8. windows的ReactNative挖坑一分钟爬坑一小时

    其实开发并不需要Android Studio来开发,因为命令行都是要自己手打的,所以就开始了我的挖坑爬坑之旅 首先安装React Native要用到的git.nodejs等等这里不讲了,主要讲在手机上 ...

  9. DevOps/TestOps概念

    天下大势分久必合合久必分,早期的软件开发只有软件工程师一人完成,为了提高效率逐渐实行分工模式:开发.测试.运维.不同角色担任不同的任务.分工越来越细之后带来了问题也越来越突出,那就是各角色之间的沟通成 ...

  10. swift内存管理中的引用计数

    在swift中,每一个对象都有生命周期,当生命周期结束会调用deinit()函数进行释放内存空间. 观察这一段代码: class Person{ var name: String var pet: P ...