<Classname> .prototype。<method>不适用于“ this”关键字

时间:2020-04-21 12:23:48

标签: javascript prototype

class Student{
    constructor(name,roll){
        this.name = name
        this.roll = roll
    }

    intro(){
        console.log('name is',this.name,' and roll number is',this.roll)
    }
}

Student.prototype.introduce = ()=> console.log('name is',this.name,' and roll number is',this.roll)

let s1 = new Student('any name',12345)
s1.intro()
s1.introduce()

问题是,介绍和介绍都已正确加载到Student.prototype中,但是介绍方法是打印出名称和名称,但是介绍在两个地方都给出了未定义的内容

任何帮助表示赞赏。谢谢!

0 个答案:

没有答案
相关问题