首頁
大數據可視化設計
B端UI設計
系統UI設計
移動端UI設計
圖標設計
軟件開發
高端網站設計
logo設計
平面設計
關于我們
關于我們
公益活動
設計每日一帖
call、apply、bind 原理實現
2020-4-6
seo達人
目錄
call 的模擬實現
apply 的模擬實現
bind 的模擬實現
三者異同
學習并參考于:
JavaScript深入之call和apply的模擬實現
JS的call,apply與bind詳解,及其模擬實現
(一)call的模擬實現
call 用法 : MDN Function.prototype.call()
call() 方法使用一個指定的 this 值和可選的參數列表來調用一個函數。
call() 提供新的 this 值給當前調用的函數/方法。
call 實現主要思路:
將函數設為對象的屬性
執行該函數
刪除該函數
另外還有考慮:
call 函數還能給定參數執行函數
this 參數不傳,或者傳null,undefined, this指向window對象
函數是可以有返回值的
實現:
Function.prototype.myCall = function () {
if (typeof this !== 'function') {
throw new TypeError('error!')
}
let context = arguments[0] || window //this 參數可以傳 null,當為 null 的時候,視為指向 window
context.fn = this // 首先要獲取調用call的函數,用this可以獲取
let args = [...arguments].slice(1) //從 Arguments 對象中取值,取出第二個到最后一個參數
let result = context.fn(...args) //函數是可以有返回值的
delete context.fn
return result
}
測試:
// 測試一下上面實現的myCall
var value = 2;
var obj = {
value: 1
}
function bar(name, age) {
console.log(this.value);
return {
value: this.value,
name: name,
age: age
}
}
bar.call(null); // 2
console.log(bar.myCall(obj, 'kevin', 18));
// 1
// Object {
// value: 1,
// name: 'kevin',
// age: 18
// }
(二)apply 的模擬實現
apply 用法:MDN Function.prototype.apply()
apply() 方法使用一個指定的 this 值和可選的參數數組 來調用一個函數。
apply 的實現跟 call 類似。
實現:
Function.prototype.myApply = function () {
if (typeof this !== 'function') {
throw new TypeError('error!')
}
let context = arguments[0] || window
context.fn = this
let result = arguments[1] ? context.fn(...arguments[1]) : context.fn()
delete context.fn
return result
}
測試:
var foo = {
value: 1
}
function bar(name, age) {
console.log(name)
console.log(age)
console.log(this.value);
}
bar.myApply(foo, ['black', '18']) // black 18 1
(三)bind 的模擬實現
bind 用法:MDN Function.prototype.bind()
bind()方法會創建一個新函數,稱為綁定函數。當這個新函數被調用時,bind() 的第一個參數將作為它運行時的 this,之后的一序列參數將會在傳遞的實參前傳入作為它的參數。
bind是ES5新增的一個方法,不會執行對應的函數,而是返回對綁定函數的引用。
實現:
Function.prototype.customBind = function () {
if (typeof this !== 'function') {
throw new TypeError('error!')
}
const that = this // 首先要獲取調用bind的函數,用this獲取并存放在that中
let context = arguments[0] || window
const args = [...arguments].slice(1)
return function() {
return that.apply(context, args.concat([...arguments]))
}
}
(四)三者異同
相同:
改變函數體內 this 的指向
不同:
call、apply的區別:call方法接受的是參數列表,而apply方法接受的是一個參數數組。
bind不立即執行。而call或apply會自動執行對應的函數。
«
TinyUI-TUIListView最簡單的使用
JavaScript 的簡述與基礎語法
»
分類
大數據可視化設計文章及欣賞(266)
B端ui設計文章及欣賞(656)
系統UI設計文章及欣賞(135)
移動端UI設計文章及欣賞(735)
圖標設計文章及欣賞(134)
網站設計文章及欣賞(490)
平面設計(281)
行業趨勢(530)
設計資源(893)
交互設計及用戶體驗(983)
前端及開發文章及欣賞(1037)
隨筆的一些文章(73)
設計思維(1979)
用戶研究(273)
設計管理與成長(441)
seo優化(405)
日歷
鏈接
個人資料
藍藍設計的小編
http://www.skdbbs.com
存檔
2025年9月(9)
2025年8月(89)
2025年7月(149)
2025年6月(41)
2025年5月(33)
2025年4月(94)
2025年3月(121)
2025年2月(80)
2025年1月(62)
2024年12月(61)
2024年11月(84)
2024年10月(167)
2024年9月(144)
2024年8月(164)
2024年7月(108)
2024年6月(64)
2024年5月(73)
2024年4月(44)
2024年3月(50)
2024年2月(58)
2024年1月(44)
2023年12月(47)
2023年11月(41)
2023年10月(14)
2023年9月(27)
2023年8月(88)
2023年7月(62)
2023年6月(58)
2023年5月(28)
2023年4月(47)
2023年3月(37)
2023年2月(90)
2023年1月(78)
2022年12月(45)
2022年11月(69)
2022年10月(51)
2022年9月(135)
2022年8月(60)
2022年7月(111)
2022年6月(162)
2022年5月(143)
2022年4月(86)
2022年3月(119)
2022年2月(53)
2022年1月(99)
2021年12月(105)
2021年11月(83)
2021年10月(101)
2021年9月(153)
2021年8月(147)
2021年7月(149)
2021年6月(157)
2021年5月(124)
2021年4月(185)
2021年3月(144)
2021年2月(35)
2021年1月(103)
2020年12月(95)
2020年11月(76)
2020年10月(31)
2020年9月(45)
2020年8月(50)
2020年7月(46)
2020年6月(33)
2020年5月(78)
2020年4月(69)
2020年3月(100)
2020年2月(59)
2020年1月(31)
2019年12月(50)
2019年11月(57)
2019年10月(48)
2019年9月(48)
2019年8月(57)
2019年7月(58)
2019年6月(58)
2019年5月(31)
2019年4月(37)
2019年3月(43)
2019年2月(25)
2019年1月(45)
2018年12月(41)
2018年11月(40)
2018年10月(29)
2018年9月(40)
2018年8月(87)
2018年7月(107)
2018年6月(86)
2018年5月(109)
2018年4月(40)
2018年3月(35)
2017年8月(35)
2017年7月(45)
2017年6月(7)
2017年5月(27)
2017年4月(51)
2017年3月(69)
2017年2月(65)
2017年1月(69)
2016年12月(55)
2016年11月(111)
2016年10月(92)
2016年9月(53)
2016年8月(9)
2016年7月(4)
2016年6月(9)
2016年3月(19)
2016年2月(26)
2016年1月(29)
2015年12月(34)
2015年11月(35)
2015年10月(46)
2015年9月(43)
2015年8月(40)
2015年7月(33)
2015年6月(46)
2015年5月(58)
2015年4月(70)
2015年3月(55)
2015年2月(17)
2015年1月(33)
2014年12月(21)
2014年11月(83)
2014年10月(94)
2014年9月(6)
2014年8月(1)
2014年7月(13)
2014年6月(66)
2014年5月(99)
2014年4月(88)
2014年3月(101)
2014年2月(67)
2014年1月(83)
2013年12月(106)
2013年11月(111)
2013年10月(61)
2013年9月(20)
2013年7月(13)
2013年6月(27)
2013年5月(48)
2013年4月(39)
2013年3月(8)
2013年2月(20)
2013年1月(31)
2012年12月(33)
2012年11月(31)
2012年10月(22)
2012年9月(8)
2012年7月(14)
2012年6月(15)
2012年5月(31)
2012年4月(24)
2012年2月(4)
2012年1月(8)
2011年12月(35)
2011年11月(32)
2011年10月(13)
2011年8月(1)
2011年6月(1)
主站蜘蛛池模板:
亚洲av无码国产精品色午夜字幕
|
国产亚洲精品国产
|
jizz国产精品网站
|
欧美国产成人久久精品
|
久久久久国产精品
|
久久国产欧美日韩精品
|
欧美精品亚洲日韩aⅴ
|
北岛玲日韩精品一区二区三区
|
久久精品国产亚洲AV无码娇色
|
久久精品国产一区二区三区
|
99久久免费国产精品热
|
伊人久久大香线蕉精品不卡
|
另类国产精品一区二区
|
国产精品伦理久久久久久
|
国产在线精品一区二区不卡
|
精品一区二区三区东京热
|
伊人精品视频在线
|
亚洲国产婷婷综合在线精品
|
久久久久这里只有精品
|
精品人妻伦一二三区久久
|
国产精品主播一区二区
|
国产成人久久精品二区三区
|
亚洲精品乱码久久久久久按摩
|
欧美精品手机在线播放
|
97久久精品午夜一区二区
|
色一乱一伦一图一区二区精品
|
国内精品51视频在线观看
|
97人妻无码一区二区精品免费
|
久久久国产乱子伦精品作者
|
无码精品国产VA在线观看
|
亚洲国产精品一区第二页
|
青青草原综合久久大伊人精品
|
国产成人精品亚洲日本在线
|
99精品视频在线观看免费
|
2020国产精品永久在线
|
久久99热国产这有精品
|
天天爽夜夜爽精品视频app
|
91麻精品国产91久久久久
|
国产精品成人观看视频网站
|
国亚洲欧美日韩精品
|
久久久久亚洲精品男人的天堂
|