1. Process of writing new by hand

const myNew = (fn, ... args) => { const obj = {} obj.__proto__ = fn.prototype fn.apply(obj, args) return obj }Copy the code

2. Instanceof keyword

const instanceOf = (father, child) => {
    const fp = father.prototype
    var cp = child.__proto__
    while (cp) {
        if (cp === fp) {
            return true
        }
        cp = cp.__proto__
    }
    return false
}
Copy the code

3. Anti-shake function

const debounce = (func, wait = 500) => { let timer = 0 return function(... args) { if (timer) clearTimeout(timer) timer = setTimeout(() => { func.apply(this, args) }, wait) } }Copy the code

4. Throttling function

const throttle = (func, wait = 500) => { let lastTime = 0 return function(... args) { let now = +new Date() if (now - lastTime > wait) { lastTime = now func.apply(this, args) } } } setInterval( throttle(() => { console.log(1) }, 3000), 1 )Copy the code

5. Handwritten call()

Function.prototype.myCall = function(context) { if (typeof this ! = = 'function') {throw new TypeError (' Error ')} context = context | | the window / / who call call method, Context.fn = this const args = [...arguments].slice(1) const result = context.fn(... arguments) const args = [...arguments].slice(1) const result = context.fn(... args) delete context.fn return result }Copy the code

6, write apply()

Function.prototype.apply = function (context) { if (typeof this ! = = 'function') {throw new TypeError (' Error ')} context = context | | window context. The fn = this let the result / / processing parameters and the call If (arguments[1]) {result = context.fn(... arguments[1]) } else { result = context.fn() } delete context.fn return result }Copy the code

7, handwritten bind()

Function.prototype.bind = function(context) { if (typeof this ! == 'function') {throw new TypeError('Error')} const _this = this const args = [...arguments].slice(1 Function F() {// If (this instanceof F) {// Ignore this return new _this(... args, ... Return _this.apply(context, args. Concat (... arguments)) } }Copy the code

See: Call, apply, and bind differences and source code parsing

8, array deduplication

// Reduce const quchong1 = (arr) => {const newArr = [] arr. Reduce ((pre, next) => {if (! Pre [next]) {pre[next] = 1 newarr. push(next)} return pre}, {}) return newArr} Set const quchong2 = (arr) => {return [...new Set(arr)]}Copy the code

9. Array objects are deduplicated

Let arr = [{key: '01, value:' beauty '}, {key: '02' value: "wang zhaojun"}, {key: '03, value: "jade bracelet"}, {key:' 04 'value: "The sable cicada"}, {key: '01, value:' beauty '}, {key: '01, value:' beauty '}]; let obj = {}; arr = arr.reduce((item, next) => { obj[next.key] ? '' : obj[next.key] = true && item.push(next); return item; } []); console.log(arr); / / [{key: "01", value: "xi shi"}, {key: "02", value: "wang zhaojun"}, {key: "3", the value: "jade bracelet"}, {key: "04", value: "the sable cicada"}]Copy the code

10, simple version of deep copy

/* * @param x {Object} Object 1 * @param y {Object} Object 2 * @return {Boolean} true */ export const deepEqual = (x, y) => {if (x === y) {return true; } else if ((typeof x == "object" && x ! = null) && (typeof y == "object" && y ! = null)) { if (Object.keys(x).length ! == Object.keys(y).length) { return false; } for (var prop in x) { if (y.hasOwnProperty(prop)) { if (! deepEqual(x[prop], y[prop])) return false; } else { return false; } } return true; } else { return false; }}Copy the code

See: In JavaScript, how to determine whether two objects are equal?