Here sort out and continue to update some common handwritten questions, for in-depth understanding of the underlying principle of JS, or surprise interview, have certain help:
Handwritten instanceof
function isInstanceof(obj, fn) {
if(! (typeof obj === 'object'&& obj ! = =null)) return false // All non-object types are false
let proto = obj // Get the prototype of the object
while ((proto = Object.getPrototypeOf(proto))) {
if (proto === fn.prototype) return true
}
return false
}
Copy the code
Handwritten Object. The create
function objectCreate(obj) {
function F() {}
F.prototype = obj
return new F()
}
Copy the code
Write a new
function New(ctor, ... args) {
if (typeofctor ! = ='function') throw `${ctor} is not a constructor`
const obj = Object.create(ctor.prototype)
const res = ctor.apply(obj, args) // If the constructor returns a value, execute it directly
const isObject = typeof res === 'object'&& res ! = =null
const isFunction = typeof res === 'function'
return isObject || isFunction ? res : obj
}
Copy the code
Hand-written Debounce anti-shake
// Common anti-shake
function debounce(fn, wait = 1000) {
let timer
return function (. args) {
if (timer) clearTimeout(timer)
timer = setTimeout(() = > {
timer = nullfn(... args) }, wait) } }// Can control whether to immediately implement the anti-shake (more practical)
function debounceImmediate(fn, wait = 2000, immediate = true) {
let timer
return function (. args) {
varrunNow = ! timer && immediateif (timer) clearTimeout(timer)
timer = setTimeout(() = > {
timer = null
if(! immediate) fn(... args) }, wait)if(runNow) fn(... args) } }Copy the code
Handwritten throttle
function throttle(fn, wait = 1000) {
let previous = 0
let timer
return function (. args) {
let now = Date.now()
let remain = wait - (now - previous)
if (remain <= 0) { fn(... args) previous = now }else if(! timer) { timer =setTimeout(() = >{ fn(... args) previous =Date.now()
timer = null
}, remain)
}
}
}
Copy the code
Handwriting compose function
async function compose(middlewares, ctx) {
let currentIndex = 0
let currentFunc = middlewares[currentIndex]
const next = async () => {
currentIndex++
if (currentIndex >= middlewares.length) return
currentFunc = middlewares[currentIndex]
return currentFunc(ctx, next)
}
return currentFunc(ctx, next)
}
Copy the code
Handwritten cluster-worker model
const cluster = require('cluster')
const http = require('http')
// If it is the main process
if (cluster.isMaster) {
console.log('is master')
// fork the worker child process
constworker = cluster.fork() ; ['error'.'exit'.'listening'.'message'.'online'].forEach(event= > {
worker.on(event, msg= > {
console.log(` [${event}] from worker:`, msg)
})
})
} else {
console.log('is worker')
http
.createServer((req, res) = > {
process.send(`${req.url}`)
res.end(`Hello World: ${req.url}`)
})
.listen(8000)}Copy the code
Handwriting flat function
function flat(arr, depth = 1) {
if (depth <= 0) return arr
const ret = []
for (let i = 0; i < arr.length; i++) {
const it = arr[i]
if (Array.isArray(it)) { ret.push(... flat(it, depth -1))}else {
ret.push(it)
}
}
return ret
}
Copy the code
TODO: Constantly updated…