zoukankan      html  css  js  c++  java
  • bind、call、apply

    bind、call、apply

    作用

    都是为了修改执行函数的 this 指向。

    区别

    bind 只是修改了 this 的指向,没有立即执行,call 和 apply 会立马执行;
    call 和 apply 区别在于第二个参数,call 为多个参数展开,apply 的第二参数为一个数组。

    实现手写

    // _bind.js
    // 别人写法
    Function.prototype._bind1 = function (context, ...args) {
        if (!context || context === null) context = window;
        const fn = Symbol();
        context[fn] = this;
        const _this = this;
        const result = function (...innerArgs) {
            if (this instanceof _this) {
                this[fn] = _this;
                this[fn](...[...args, ...innerArgs]);
                delete this[fn];
            } else {
                context[fn](...[...args, ...innerArgs]);
                delete context[fn];
            }
        };
        result.prototype = Object.create(this.prototype);
        return result;
    };
    
    // 官方写法
    Function.prototype._bind2 = function () {
        const _this = this;
        const realThis = arguments[0] ? arguments[0] : window;
        const restParams = arguments && arguments.slice(1);
        if (typeof _this !== 'function') {
            throw new TypeError('Function.prototype.bind - ' + 'what is trying to be bound is not callable');
        }
        return function () {
            _this.call(realThis, ...restParams);
        };
    };
    
    
    /*
    将函数设为对象的属性
    执行该函数
    删除该函数
    */
    Function.prototype._call = function () {
        if (typeof this !== 'function') {
            throw new TypeError('must be a function ');
        }
        const _this = arguments[0] || window; // 获取this参数,如果没有参数或者参数为null时,this指向window
        const params = arguments.slice(1); // 获取函数执行的参数。
        const fn = new Symbol(); // 唯一性
        _this[fn] = this;
        const ret = _this[fn](...params); // 带参数执行,
        delete _this[fn]; // 删除替代属性
        return ret; // 返回执行结果。
    };
    
    
    Function.prototype._apply = function () {
        if (typeof this !== 'function') {
            throw new TypeError('must be a function ');
        }
        const _this = arguments[0] || window; // 获取this参数,如果没有参数或者参数为null时,this指向window
        const params = arguments.slice(1) || []; // 获取函数执行的参数。
        const fn = new Symbol(); // 唯一性
        _this[fn] = this;
        const ret = _this[fn](params); // 带参数执行,
        delete _this[fn]; // 删除替代属性
        return ret; // 返回执行结果。
    };
    
  • 相关阅读:
    HDU 4920 Matrix multiplication
    UVALive 5545 Glass Beads
    POJ 2230 Watchcow
    hdu 1878 欧拉回路
    hdu 3018 Ant Trip
    2015 Multi-University Training Contest 1 hdu 5296 Annoying problem
    深入理解php内核 编写扩展 I:介绍PHP和Zend
    PHP数组实际占用内存大小的分析
    深入理解php底层:php生命周期
    PHP实现的进度条效果详解
  • 原文地址:https://www.cnblogs.com/tutao1995/p/15252636.html
Copyright © 2011-2022 走看看