亚洲av日韩av永久无码久久,免费的视频app网站,欧美性猛交xxxx免费看,国产成人精品三级麻豆
首頁(yè)
大數(shù)據(jù)可視化設(shè)計(jì)
B端UI設(shè)計(jì)
系統(tǒng)UI設(shè)計(jì)
移動(dòng)端UI設(shè)計(jì)
圖標(biāo)設(shè)計(jì)
軟件開發(fā)
高端網(wǎng)站設(shè)計(jì)
logo設(shè)計(jì)
平面設(shè)計(jì)
關(guān)于我們
關(guān)于我們
公益活動(dòng)
設(shè)計(jì)每日一帖
call、apply、bind 原理實(shí)現(xiàn)
2020-4-6
seo達(dá)人
目錄
call 的模擬實(shí)現(xiàn)
apply 的模擬實(shí)現(xiàn)
bind 的模擬實(shí)現(xiàn)
三者異同
學(xué)習(xí)并參考于:
JavaScript深入之call和apply的模擬實(shí)現(xiàn)
JS的call,apply與bind詳解,及其模擬實(shí)現(xiàn)
(一)call的模擬實(shí)現(xiàn)
call 用法 : MDN Function.prototype.call()
call() 方法使用一個(gè)指定的 this 值和可選的參數(shù)列表來(lái)調(diào)用一個(gè)函數(shù)。
call() 提供新的 this 值給當(dāng)前調(diào)用的函數(shù)/方法。
call 實(shí)現(xiàn)主要思路:
將函數(shù)設(shè)為對(duì)象的屬性
執(zhí)行該函數(shù)
刪除該函數(shù)
另外還有考慮:
call 函數(shù)還能給定參數(shù)執(zhí)行函數(shù)
this 參數(shù)不傳,或者傳null,undefined, this指向window對(duì)象
函數(shù)是可以有返回值的
實(shí)現(xiàn):
Function.prototype.myCall = function () {
if (typeof this !== 'function') {
throw new TypeError('error!')
}
let context = arguments[0] || window //this 參數(shù)可以傳 null,當(dāng)為 null 的時(shí)候,視為指向 window
context.fn = this // 首先要獲取調(diào)用call的函數(shù),用this可以獲取
let args = [...arguments].slice(1) //從 Arguments 對(duì)象中取值,取出第二個(gè)到最后一個(gè)參數(shù)
let result = context.fn(...args) //函數(shù)是可以有返回值的
delete context.fn
return result
}
測(cè)試:
// 測(cè)試一下上面實(shí)現(xiàn)的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 的模擬實(shí)現(xiàn)
apply 用法:MDN Function.prototype.apply()
apply() 方法使用一個(gè)指定的 this 值和可選的參數(shù)數(shù)組 來(lái)調(diào)用一個(gè)函數(shù)。
apply 的實(shí)現(xiàn)跟 call 類似。
實(shí)現(xiàn):
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
}
測(cè)試:
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 的模擬實(shí)現(xiàn)
bind 用法:MDN Function.prototype.bind()
bind()方法會(huì)創(chuàng)建一個(gè)新函數(shù),稱為綁定函數(shù)。當(dāng)這個(gè)新函數(shù)被調(diào)用時(shí),bind() 的第一個(gè)參數(shù)將作為它運(yùn)行時(shí)的 this,之后的一序列參數(shù)將會(huì)在傳遞的實(shí)參前傳入作為它的參數(shù)。
bind是ES5新增的一個(gè)方法,不會(huì)執(zhí)行對(duì)應(yīng)的函數(shù),而是返回對(duì)綁定函數(shù)的引用。
實(shí)現(xiàn):
Function.prototype.customBind = function () {
if (typeof this !== 'function') {
throw new TypeError('error!')
}
const that = this // 首先要獲取調(diào)用bind的函數(shù),用this獲取并存放在that中
let context = arguments[0] || window
const args = [...arguments].slice(1)
return function() {
return that.apply(context, args.concat([...arguments]))
}
}
(四)三者異同
相同:
改變函數(shù)體內(nèi) this 的指向
不同:
call、apply的區(qū)別:call方法接受的是參數(shù)列表,而apply方法接受的是一個(gè)參數(shù)數(shù)組。
bind不立即執(zhí)行。而call或apply會(huì)自動(dòng)執(zhí)行對(duì)應(yīng)的函數(shù)。
«
TinyUI-TUIListView最簡(jiǎn)單的使用
JavaScript 的簡(jiǎn)述與基礎(chǔ)語(yǔ)法
»
分類
大數(shù)據(jù)可視化設(shè)計(jì)文章及欣賞(158)
B端ui設(shè)計(jì)文章及欣賞(622)
系統(tǒng)UI設(shè)計(jì)文章及欣賞(120)
移動(dòng)端UI設(shè)計(jì)文章及欣賞(705)
圖標(biāo)設(shè)計(jì)文章及欣賞(122)
網(wǎng)站設(shè)計(jì)文章及欣賞(484)
平面設(shè)計(jì)(258)
行業(yè)趨勢(shì)(466)
設(shè)計(jì)資源(875)
交互設(shè)計(jì)及用戶體驗(yàn)(925)
前端及開發(fā)文章及欣賞(1031)
隨筆的一些文章(63)
設(shè)計(jì)思維(1912)
用戶研究(244)
設(shè)計(jì)管理與成長(zhǎng)(384)
seo優(yōu)化(498)
日歷
鏈接
個(gè)人資料
藍(lán)藍(lán)設(shè)計(jì)的小編
http://www.chinatradebeam.com
存檔
2025年3月(47)
2025年2月(81)
2025年1月(66)
2024年12月(61)
2024年11月(84)
2024年10月(168)
2024年9月(145)
2024年8月(165)
2024年7月(108)
2024年6月(65)
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)
偷窥学校女厕撒尿bbbbb
|
风韵丰满熟妇啪啪区老熟熟女
|
久久99精品久久久久久水蜜桃
|
国产老熟女精品一区
|
高雅人妻被迫沦为玩物
|
女人和公猪交内射
|
久久夜色精品国产欧美乱
|
久久人妻熟女中文字幕av蜜芽
|
国产96在线 | 亚洲
|
无码人妻精品一区二区三区夜夜嗨
|
激情97综合亚洲色婷婷五
|
欧美喷潮久久久xxxxx
|
成人h动漫精品一区二区无码
|
丝袜美腿一区二区三区
|
两根大肉大捧一进一出好爽视频
|
亚洲AV无码一区二区二三区入口
|
国产av人人夜夜澡人人爽麻豆
|
国产综合久久久久
|
色综合久久88色综合天天
|
中文字幕av一区
|
美女班主任露出奶头喂我乳我
|
国产精品国产精品国产专区不卡
|
国产绳艺sm调教室论坛
|
久久久久亚洲av无码专区网站
|
国内揄拍国内精品少妇国语
|
精品久久久久久综合日本
|
色一情一乱一伦一区二区三欧美
|
亚洲欧美精品午睡沙发
|
人妻少妇精品无码专区二区
|
男男情趣玩具play高h
|
天堂网在线最新版www中文网
|
青青草视频在线
|
a片毛片免费看
|
色国产精品一区在线观看
|
国产麻豆一精品一av一免费
|
午夜精品久久久久久久无码
|
免费观看a级毛片
|
在床上拔萝卜又疼又叫什么病
|
理科生坠入情网电视剧免费观看
|
人妻少妇久久久久久97人妻
|
国产av一区二区三区日韩
|