注:本篇博客续上一篇“异步编程函数式编程底层代码手写”,因此编号从13开始
13. instanceof操作符的实现
function imitateInstanceof(left, right) {
"use strict";
if (right && !right[Symbol.hasInstance])
throw new TypeError("类型检查函数的第二个参数必须是类")
if(!left || left && typeof left !== "object" && typeof left !== "function")
return false
let leftProto = Object.getPrototypeOf(left)
let rightProto = right.prototype;
return (function check() {
if(!leftPrototype)
return false
else if(leftPrototype === rightPrototype)
return true
else {
leftPrototype = Object.getPrototypeOf(leftPrototype)
return check()
}
})();
}
14.new操作符的实现
function imitateNew(constructor, ...args) {
if(typeof constructor !== "function")
throw new TypeError("构造函数必须是函数")
let target = Object.create(constructor.prototype)
let result = constructor.call(target, ...args)
Object.setPrototypeOf(target, constructor.prototype)
return result instanceof Object ? result : target
}
15.对象浅拷贝实现
function copy(obj) {
obj = obj instanceof Object ? obj : Object(obj)
let target = null, key
if(obj instanceof Array)
target = []
else if(obj instanceof Function)
return obj
else
target = new Object
for([key, target[key]] of Object.entries(obj));
for(let p of Object.getOwnPropertySymbols(obj))
if(Object.getOwnPropertyDescriptor(obj, p).enumerable)
target[p] = obj[p]
return target
}
16.Object.assign()实现
Object.$assign = function(target, ...sources) {
target = target instanceof Object ? target : Object(target)
sources = sources.map(el => el instanceof Object ? el : Object(el))
let key
sources.forEach(obj => {
for([key, target[key]] of Object.entries(obj));
for(let p of Object.getOwnPropertySymbols(obj))
if(Object.getOwnPropertyDescriptor(obj, p).enumerable)
target[p] = obj[p]
})
return target
}
17.扩展操作符合并对象实现
Object.$expand = function(target, ...sources) {
target = target instanceof Object ? target : Object(target)
sources = sources.map(el => el instanceof Object ? el : Object(el))
let res = {}
function copy(obj) {
for(let [key, value] of Object.entries(obj))
Object.defineProperty(res, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
})
for(let p of Object.getOwnPropertySymbols(target))
if(Object.getOwnPropertyDescriptor(target, p).enumerable)
Object.defineProperty(obj, p, {
value: target[p],
enumerable: true,
configurable: true,
writable: true
})
}
sources.forEach(copy)
copy(target)
return res
}
18.函数的深复制(实际应用中没有任何意义,实现纯属娱乐)
function copyFunction(target) {
let func = target.toString(), args, body
if(target.prototype || func.startsWith(`${target.name}`)) {
args = func.slice(func.indexOf("(") + 1, func.indexOf(")")).split(",")
body = func.slice(func.indexOf("{") + 1, func.lastIndexOf("}"))
body = body.replaceAll("this." + target.name, "arguments.callee")
} else {
if(func.startsWith("("))
args = func.slice(func.indexOf("(") + 1, func.indexOf(")")).split(",")
else
args = func.slice(0, func.indexOf("=>")).trim().split(",")
if(func.endsWith("}"))
body = func.slice(func.indexOf("{") + 1, func.lastIndexOf("}"))
else
body = "return " + func.slice(func.indexOf("=>") + 2).trim()
}
return new Function(...args, body)
}
19.对象的深复制
function deepClone(obj) {
let wm = new WeakMap()
function copy(key, value, target, obj) {
if (value instanceof Object) {
let descriptor = Object.getOwnPropertyDescriptor(obj, key)
descriptor.value = clone(value)
Object.defineProperty(target, key, descriptor)
} else
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(obj, key))
}
function clone(obj) {
obj = obj instanceof Object ? obj : Object(obj)
if (obj instanceof Date) return new Date(obj)
if (obj instanceof RegExp) return new RegExp(obj)
if (wm.has(obj))
return wm.get(obj)
let target
if (typeof obj === "object")
target = new obj.constructor()
else if (typeof obj === "function")
return obj
else if (Array.isArray(obj))
target = new Array
wm.set(obj, target)
Object.entries(obj).forEach(([key, value]) => copy(key, value, target, obj))
Object.getOwnPropertySymbols(obj).forEach(key => {
if (Object.getOwnPropertyDescriptor(obj, key).enumerable)
copy(key, obj[key], target, obj)
})
return target
}
return clone(obj)
}
|