Function.prototype.mycall = function(context, ...args) {
context = context ? new Object(context) : window
let key = Symbol()
context[key] = this
context[key](...args)
delete context[key]
}
Function.prototype.myapply = function(context) {
context = context || window
let key = Symbol()
let args = arguments[1]
context[key] = this
let result
if (args) {
result = context[key](...args)
} else {
result = context[key]()
}
delete context[key]
}
Function.prototype.mybind = function(context) {
const self = this
let args = arguments.slice(1)
const bindFn = function() {
return self.apply(
this instanceof bindFn ? this : context,
...args,
...arguments
)
}
bindFn.prototype = Object.create(self.prototype)
return bindFn
}
// 模擬new
const createNew = (Con, ...args) => {
const obj = Object.create(Con.prototype)
// Object.setPrototypeOf(obj, Con.prototype)
let result = Con.apply(obj, args)
return result instanceof Object ? result : obj
}
//模擬instance
const myInstanceOf = (left, right) => {
let leftValue = left.__proto__
let rightValue = right.prototype
while (true) {
if (leftValue === null) return false
if (leftValue === rightValue) return true
leftValue = leftValue.__proto__
}
}
//深拷貝
const deepClone = (target, cache = new WeakMap()) => {
if (target === null || typeof target !== 'object') {
return target
}
if (cache.get(target)) {
return target
}
const copy = Array.isArray(target) ? [] : {}
cache.set(target, copy)
Object.keys(target).forEach(
key => (copy[key] = deepClone(target[key], cache))
)
return copy
}
//防抖
const debounce = (fn, wait = 300, leading = true) => {
let timerId, result
return function(...args) {
timerId && clearTimeout(timerId)
if (leading) {
if (!timerId) {
result = fn.apply(this, args)
timerId = setTimeout(() => {
timerId = null
}, wait)
}
} else {
timerId = setTimeout(() => {
result = fn.apply(this, args)
timerId = null
}, wait)
}
return result
}
}
//節(jié)流-定時器
const throttle = (fn, wait = 300) => {
let timerId
return function(...args) {
if (timerId) {
setTimeout(() => {
return fn.apply(this, args)
timerId = null
}, wait)
}
}
}
//節(jié)流-時間戳
const throttleByTimer = (fn, wait = 300) => {
let prev = 0
return function(...args) {
let now = +new date()
if (now - prev > wait) {
prev = now
return fn.apply(this, args)
}
}
}
//數組去重
const uniqBy = (arr, key) => {
return [...new Map(arr.map(item => [item[key], item])).values()]
}
//數組扁平化-技巧版
const flatten = arr =>
arr
.toString()
.split(',')
.map(item => +item)
//數組扁平化
const flatten2 = (arr = [], deep = 1) => {
return arr.reduce((cur, next) => {
return Array.isArray(next) && deep > 1
? [...cur, ...flatten2(next, deep - 1)]
: [...cur, next]
}, [])
}
//函數柯里化
const currying = fn =>
(_curry = (...args) =>
args.length >= fn.length
? fn(...args)
: (...newArgs) => _curry(...args, ...newArgs))
//發(fā)布訂閱
class EventEmitter {
subs = {}
emit(event, ...args) {
if (this.subs[event] && this.subs[event].length) {
this.subs[event].forEach(cb => cb(...args))
}
}
on(event, cb) {
;(this.subs[event] || (this.subs[event] = [])).push(cb)
}
off(event, offCb) {
if (offCb) {
if (this.subs[event] && this.subs[event].length)
this.subs[event] = this.subs[event].filter(cb => cb !== offCb)
} else {
this.subs[event] = []
}
}
}
手寫代碼
?著作權歸作者所有,轉載或內容合作請聯系作者
- 文/潘曉璐 我一進店門赃春,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人骄崩,你說我怎么就攤上這事聘鳞”「ǎ” “怎么了要拂?”我有些...
- 正文 為了忘掉前任拉一,我火速辦了婚禮采盒,結果婚禮上,老公的妹妹穿的比我還像新娘蔚润。我一直安慰自己磅氨,他們只是感情好,可當我...
- 文/花漫 我一把揭開白布嫡纠。 她就那樣靜靜地躺著烦租,像睡著了一般。 火紅的嫁衣襯著肌膚如雪除盏。 梳的紋絲不亂的頭發(fā)上叉橱,一...
- 文/蒼蘭香墨 我猛地睜開眼抡句,長吁一口氣:“原來是場噩夢啊……” “哼糕再!你這毒婦竟也來了?” 一聲冷哼從身側響起玉转,我...
- 正文 年R本政府宣布蟋恬,位于F島的核電站,受9級特大地震影響趁冈,放射性物質發(fā)生泄漏歼争。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一渗勘、第九天 我趴在偏房一處隱蔽的房頂上張望沐绒。 院中可真熱鬧,春花似錦旺坠、人聲如沸乔遮。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽蹋肮。三九已至,卻和暖如春蝉衣,著一層夾襖步出監(jiān)牢的瞬間括尸,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內容
- https://www.cnblogs.com/zaoansijia/p/4513897.html.myslide...
- storyboard就是之前的nib文件雀摘。好處是可以通過拖拽UI的方式來布局。但是我的學習過程走的是另外一種方式八拱,...
- 【8.3感賞日記】今天就要返程了肌稻,即將結束這一快樂的旅程清蚀,海南島的海景海鮮、重慶穿越大樓的地鐵爹谭、四川的美食美景會永...
- 1108朱海燕 Jody 作者Anthony Browne非常有幽默感:一件開滿鮮花的睡袍枷邪、一雙帶毛球的粉色拖鞋、...