instance
function instance_of (L, R) {
// 验证如果为基本数据类型,就直接返回 false
const baseType = ['string', 'number', 'boolean', 'undefined', 'symbol']
if(baseType.includes(typeof(L))) { return false }
let RP = R.prototype; // 取 R 的显示原型
L = L.__proto__; // 取 L 的隐式原型
while (true) {
if (L === null) { // 找到最顶层
return false;
}
if (L === RP) { // 严格相等
return true;
}
L = L.__proto__; // 没找到继续向上一层原型链查找
}
}
flat
const arr = [1, 2, 3, 4, [1, 2, 3, [1, 2, 3, [1, 2, 3]]], 5, "string", { name: "弹铁蛋同学" }];
// concat + 递归
flat(arr)
function flat(arr) {
debugger
let arrResult = [];
arr.forEach(item => {
if (Array.isArray(item)) {
//arrResult = arrResult.concat(arguments.callee(item)); // 递归
// 或者用扩展运算符
arrResult.push(...arguments.callee(item));
} else {
arrResult.push(item);
}
});
console.log(arrResult);
return arrResult;
}
New
function myNew(con,...args){
let obj = {};
Object.setPrototypeOf(obj,con.prototype);// => obj._proto_ = con.prototype;
let res = con.apply(obj,rags);
return res instance of Object?res : objl;
}