【转】

前言

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

原代码

  1. if(!Function.prototype.softBind){
  2. Function.prototype.softBind = function(obj){
  3. var fn = this;
  4. var curried = [].slice.call(arguments,1);
  5. var bound = function(){
  6. return fn.apply(
  7. (!this || this === (window || global)) ? obj:this,
  8. curried.concat.apply(curried,arguments);
  9. )
  10. };
  11. bound.prototype = Object.create(fn.prototype);
  12. return bound;
  13. }
  14. }

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

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


个人见解

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

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

    总结

    1. 那个写法就是裁切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. 初识homebrew

    homebrew是MAC上的一个包管理工具,用于软件安装,非常方便. homebrew安装: 命令行执行: ruby -e "$(curl -fsSL https://raw.githubu ...

  2. HTML5 Canvas:初始Canvas

    Canvas ,HTML 5中引入它,可以做很多事情:画图.动画.游戏开发等等. Canvas 元素 Canvas 中文翻译为:画布. <canvas id=”yourCanvasId” wid ...

  3. .Net6种成员的可访问性

    CLR术语 C#术语 描述 Private private 成员只能由定义类型或任何嵌套类型访问 Family protected 成员只能由定义类型,任何嵌套类型或者不管在任何程序集中声明的派生类型 ...

  4. 《Java NIO (中文版)》【PDF】下载

    <Java NIO (中文版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062530 NIO (中文版)>[PDF]& ...

  5. rabbitmq配置镜像模式

    学习是spring cloud的时候用到了rabbitmq,在实际项目中也用到了,镜像模式是集群的基础上面配置的,就多了一个数据同步,rabbitmq存储消息用的是cookie,配置之前先同步cook ...

  6. 迷宫问题 Maze 4X4 (sloved by backtrack)

    Description 给定一个N*N的迷宫中,(0,0)为起始点,(N-1,N-1)为目的地,求可通往目的地的多个解 思路 这道题其实就是简单的DFS,一路探索到底,没路就回溯到交叉口. #incl ...

  7. iOS 模拟器安装应用

    iOS模拟器是苹果Xcode IDE的一部分,主要用来为Mac,iPhone和iPad创建应用程序,为了给iOS模拟器打包应用程序,利用–package 在命令行上执行ADT并使用–target来指定 ...

  8. KVO的内部实现以及使用

    转载自:http://www.cocoachina.com/applenews/devnews/2014/0107/7667.html   KVO是实现Cocoa Bindings的基础,它提供了一种 ...

  9. NSQ之粗读浅谈

    回顾: 以前一直是C++开发(客户端),最近听同事讲go语言不错,随后便决定先从go语法开始投向go的怀抱.由于历史原因学习go语法时,用了半天的时间看完了菜鸟教程上相关资料,后来又看了易百教程上的一 ...

  10. LeetCode中的最大子串和问题(Maximum Subarray)

    问题描述: Find the contiguous subarray within an array (containing at least one number) which has the la ...