某小厂前端面试题,考点,关于this

本文主要探讨了在JavaScript中this关键字的用法及其在不同场景下的指向问题,包括函数调用、方法调用、构造函数以及箭头函数等情况下this的指向。通过对实际面试题的分析,帮助读者深入理解this的精髓,提升前端面试的准备效率。
摘要由CSDN通过智能技术生成
//补充注释区块的代码

class m {
    static instance(){
        //返回实例
    }
    nameA(){
        //返回调用的实例名称
    }
    static nameB(){
        //返回调用的类名
    }
}
class A extends m {}
class B extends m {}

A.instance().nameA()   //'A'
B.nameB()  //'B'
答案:
class m {
    static instance(){
        //返回实例
        return new this
    }
    nameA(){
        //返回调用的实例名称
        return this.constructor.name
    }
    static nameB(){
        //返回调用的类名
        return this.name
    }
}
考点:this,第一个函数里的this是类对象即A,所以new this 即new A;第二个函数里的this是new A得到的实例对象;第三个函数里的this即调用的类,即B



//链表,补充注释处代码

class Link{
    static fromAry(ary){
        let head=new this;
        ary.reduce((pre,data)=>{
            let node=new this;
            pre.next=node
            node.data=data
            return node
        },head)
        return head
    }
    toAry(){
        let ary=[]
        for(let node=this.next;;node=node.next){
            if(!node)break;
            ary.push(node.data)
        }
        return ary
    }
    length(){
        //链表长度
    }
    sort(){
        //链表排序
    }
}

let link=Link.fromAry([1,6,3,4,2])
link.sort() //1->2->3->4->6
答案:
class Link{
    static fromAry(ary){
        let head=new this;
        ary.reduce((pre,data)=>{
            let node=new this;
            pre.next=node
            node.data=data
            return node
        },head)
        return head
    }
    toAry(){
        let ary=[]
        for(let node=this.next;;node=node.next){
            if(!node)break;
            ary.push(node.data)
        }
        return ary
    }
    length(){
        //链表长度
        return this.toAry().length
    }
    sort(){
         //链表排序
         let sortAry= this.toAry().sort((a,b)=>{return a-b})
         return Link.fromAry(sortAry)
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值