zoukankan      html  css  js  c++  java
  • [].slice.call(arguments,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    /*我们自身也可以模拟一个对象传入,我们这里用数组对象,不是等同,只是道理差不多的*/
      16    aargument =  [1,2,3,4];
      17    [].slice.call(aargument,3);   //返回[4]
      18    [].slice.call(aargument,1,3); //[2, 3]

      总结

      那个写法就是裁切arguments传入的参数,保存起来操作;
  • 相关阅读:
    最短路径问题
    这是我的第一篇博客
    Time Series Anomaly Detection
    R-CNN, Fast R-CNN, Faster R-CNN, Mask R-CNN
    database 学习
    AWS Cloud Practioner 官方课程笔记
    Cassandra commands
    go 语言学习
    [Udemy] ES 7 and Elastic Stack
    第9章 内联函数
  • 原文地址:https://www.cnblogs.com/oklfx/p/8127373.html
Copyright © 2011-2022 走看看