最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

原生js如何實現(xiàn)call,apply以及bind

 更新時間:2021年04月27日 10:58:09   作者:guo&qi  
這篇文章主要介紹了原生js實現(xiàn)call,apply以及bind,幫助大家更好的理解和學(xué)習(xí)使用JavaScript,感興趣的朋友可以了解下

1、實現(xiàn)call

步驟:

  1. 將函數(shù)設(shè)為對象的屬性;
  2. 指定this到函數(shù),并傳入給定參數(shù)執(zhí)行函數(shù);
  3. 執(zhí)行之后刪除這個函數(shù);
  4. 如果不傳入?yún)?shù),默認指向window;
Function.prototype.mycall = function (context, ...args) {
    //判斷是否為函數(shù),如果不是函數(shù),則報錯
    if (typeof this !== "function") {
        throw new Error("不是函數(shù)");
    }
    context = context || window;
    context.fn = this;
    const res = context.fn(...args);
    delete context.fn;
    return res;
}

  測試代碼:

var name = "李輝", age = 25;
var obj = {
    name: "周果",
    objAge: this.age,
    myFun: function (fm, to) {
        console.log(`名字:${this.name},年齡:${this.age},來自:${fm},去往:${to}`)
    }
};
var person = {
    name: "弟弟",
    age: 12,
};

Function.prototype.mycall = function (context, ...args) {
    //判斷是否為函數(shù),如果不是函數(shù),則報錯
    if (typeof this !== "function") {
        throw new Error("不是函數(shù)");
    }
    context = context || window;
    context.fn = this;
    const res = context.fn(...args);
    delete context.fn;
    return res;
}

obj.myFun.mycall(person, "成都", "仁壽"); //名字:弟弟,年齡:12,來自:成都,去往:仁壽

2、實現(xiàn)apply

Function.prototype.myApply = function (context, ...args) {
    //判斷是否為函數(shù),如果不是函數(shù),則報錯
    if (typeof this !== "function") {
        throw new Error("不是函數(shù)");
    }
    context = context || window;
    context.fn = this;
    args = args && args[0] || [];
    const result = context.fn(...args);
    delete context.fn;
    return result;
}

  測試代碼:

obj.myFun.myApply(person, ["成都", "仁壽"]); //名字:弟弟,年齡:12,來自:成都,去往:仁壽

3、實現(xiàn)bind

  bind()方法主要就是將函數(shù)綁定到某個對象,bind()會創(chuàng)建一個函數(shù),函數(shù)體內(nèi)的this對象的值會被綁定到傳入bind()中的第一個參數(shù)的值。

  方法1:使用apply

Function.prototype.myBind = function () {
    let self = this; //保存原函數(shù)
    let context = [].shift.call(arguments); //保存需要綁定的this上下文
    let args = [...arguments]; //將傳入的剩余參數(shù)轉(zhuǎn)換成數(shù)組
    return function () {                 //返回一個新的函數(shù)
        self.apply(context,[].concat.call(args,[...arguments]));
    }
}

  ES6簡化一下:

Function.prototype.myBind = function (context, ...args1) {
        return (...args2) => {  //返回箭頭函數(shù), this綁定調(diào)用這個方法的函數(shù)對象
            context = context || window;
            return this.apply(context, args1.concat(args2));//合并參數(shù)
        }
    }

  方法2:不使用call以及apply

  將上面的代碼和js手寫實現(xiàn)apply的代碼合并一下:

Function.prototype.myBind = function (context, ...args1) {
    return (...args2) => {  //返回箭頭函數(shù), this綁定調(diào)用這個方法的函數(shù)對象
        context = context || window;
        context.fn = this;
        const args = args1.concat(args2);
        const res = context.fn(...args);
        delete context.fn;
        return res;
    }
}

  測試代碼:

obj.myFun.myBind(person, "成都", "仁壽")();//名字:弟弟,年齡:12,來自:成都,去往:仁壽

以上就是原生js如何實現(xiàn)call,apply以及bind的詳細內(nèi)容,更多關(guān)于js實現(xiàn)call,apply以及bind的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

鄂尔多斯市| 正宁县| 来安县| 龙山县| 西盟| 若尔盖县| 砀山县| 墨竹工卡县| 正宁县| 石狮市| 宝应县| 鲜城| 工布江达县| 陕西省| 泗洪县| 开平市| 密山市| 民乐县| 米脂县| 宁安市| 昌乐县| 靖西县| 惠来县| 通山县| 上虞市| 肥城市| 北安市| 扎囊县| 澎湖县| 疏勒县| 湖口县| 滨海县| 易门县| 牟定县| 类乌齐县| 大同县| 抚顺县| 湖州市| 中西区| 色达县| 卫辉市|