手写方法集合

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;
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值