JS知识点

js部分

  • 不借助第三者交换a,b的值
[a,b] = [b,a]



  • new的实现过程

function myNew(fn,...args){
    var obj = {}
    let res = fn.call(obj,args)
    obj._proto_ =  fn.prototype
    return  res instanceof Object ? res : obj
}


// let Test = new test()
function Car(color) {
    this.color = color;
    return 'a'
}
function myNew(fn,...args){
    var obj = {}
    let res = fn.apply(obj,args)
    obj.__proto__ = fn.prototype

    // 防止构造函数返回非对象
    return res instanceof Object ? res : obj


}
var c = myNew(Car,'red')
console.log(c.color); // red



继承

  • 直接prototype模式继承
function inherit(child,father){
    // 缺点改变了其构造函数
    child.prototype === father.prototype
    // 每个prototype都有一个constructor指向函数的构造函数
    child.prototype.constructor = child
}

let c = new child()
c.constructor = child // true

// 如果不加child.prototype.constructor = father
c.constructor = father // true
c明明是child构造函数生成的这里的constructor却只向了father导致继承紊乱


上述缺点:
二者都指向了同一个对象,任何对child.prototype修改,就会反应到father.prototyp上来

  • 完美继承,利用控对象
function inherit(child,father){
    function temp(){} //控对象省内存
    temp.prototype = father.prototype
    child.prototype = new temp()
    child.prototype.constructor = child
}

上述怎么实现继承的

let c = new child()
c.age

现在c上找有没有age,没有顺着原型链c._proto_(construcort.prototype)去找
constructor.prototype = new temp()
new temp().age没有
new temp()._proto_ = temp.prototype上找
如此实现继承




实现promise.all

function myPromiseAll(promiseArr){
    // 为了让传入的值不是promise也返回promise
    return new Promise((resolve,reject) => {
        if(!Array.isArray(promiseArr)){
            throw('promiseArr必须为数组')
        }
        let resArr = []
        let len = promiseArr.length;
        let count = 0;
        for(let i = 0; i < len; i++){
            // Promise.resolve将数组中非promise转为promise
            Promise.resolve(promiseArr[i])
            .then(value => {
                count++
                resArr[i] = value
                
                if(count == len) return resolve(resArr)
            })
            .catch(err => {
                return reject(err)
            })
           
        }
    })
    
}

let p1 = Promise.resolve(1)
let p2 = Promise.resolve(2)
let p3 = Promise.resolve(3)

myPromiseAll([p1,p3,p2]).then(res => {
    console.log(res); // [1,3,2]
    
})

数组去重复

  • Es6
funtion uniq(arr){
    return [...new Set(arr)]
}

  • includes

function uniq(arry) {
    var res = [];
    for (var i = 0; i < arry.length; i++) {
        if (!res.includes(arry[i])) {
            res.push(arry[i])
        }
    }
    return res;
}


  • reduce
funtion unit(arr){
    arr.reduce((pre,cur) => {
        pre.includes(cur) ? pre : pre.push(cur)
    },[])
}


this问题

var obj = {
    hi: function(){
        console.log(this);
        return ()=>{
            console.log(this);
        }
    },
    sayHi: function(){
        return function() {
            console.log(this);
            return ()=>{
                console.log(this);
            }
        }
    },
    see: function() {
        console.log(this);
        (function(){
            console.log(this)
        })()
    },
    say: ()=>{
        console.log(this);
    }
}
let hi = obj.hi(); // obj
hi();  // obj          
let sayHi = obj.sayHi();
let fun1 = sayHi(); // window
fun1();             //  window
obj.say();//  window(一直向上找)
obj.see(); // obj  window

箭头函数与普通函数区别

  • 箭头函数没有自己的this, 所有不能用new来构造
  • 没有prototype属性
  • 不绑定arguments
var foo = (...args) => {
  return args[0]
}
console.log(foo(1)) 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值