function deepClone(obj, hash = new WeakMap()) {
if(obj instanceof RegExp) {
return new RegExp(obj);
}
if(obj instanceof Date) {
return new Date(obj);
}
if(obj === null || typeof obj !== 'object') {
return obj;
}
if(hash.has(obj)) {
return hash.get(obj);
}
let constr = new obj.constructor();
hash.set(obj, constr);
for(let key in obj) {
if(obj.hasOwnProperty(key)) {
constr[key] = deepClone(obj[key], hash);
}
}
let symbolObj = Object.getOwnPropertySymbols(obj);
for(let i = 0; i < symbolObj.length; i++) {
if(obj.hasOwnProperty(symbolObj[i])) {
constr[symbolObj[i]] = deepClone(obj[symbolObj[i]], hash);
}
}
return constr;
}
function sum(a, b, c) {
return a + b + c;
}
function curry(fn) {
return function currify() {
const args = Array.prototype.slice.call(arguments);
return args.length >= fn.length
? fn.apply(null, args)
: currify.bind(null, ...args);
}
}
let currySum = curry(sum);
function objSort(obj) {
let newObj = {};
Object.keys(obj).sort().map(key => {
newObj[key] = obj[key];
})
return JSON.stringify(newObj);
}
const obj = [{a:1,b:2,c:3},{b:2,c:3,a:1},{d:2,c:2}];
function unique(arr) {
let set = new Set();
for(let i = 0; i < arr.length; i++) {
let str = objSort(arr[i]);
set.add(str);
}
arr = [...set].map(item => {
return JSON.parse(item);
})
return arr;
}
let res = unique(obj);
function myInstanceof(left, right) {
let proto = Object.getPrototypeOf(left);
let prototype = right.prototype;
while(true) {
if(!proto) {
return false;
}
if(proto === prototype) {
return true;
}
proto = Object.getPrototypeOf(proto);
}
}
let Obj1 = function() {
}
let test1 = {};
res = myInstanceof(test1, Obj1);
function Ctor() {
}
function myNew(ctor, ...args) {
if(typeof ctor !== 'function') {
throw 'mynew function the first param must be a function';
}
let newObj = Object.create(ctor.prototype);
let ctorReturnResult = ctor.apply(newObj, args);
let isObj = typeof ctorReturnResult === 'object'
&& ctorReturnResult !== null;
let isFunction = typeof ctorReturnResult === 'function';
if(isObj || isFunction) {
return ctorReturnResult;
}
return newObj;
}
let c = myNew(Ctor);
console