es6(class)

作者: 余生筑 | 来源:发表于2017-10-24 09:13 被阅读10次

js中的私有方法

_privateMethod(){}形式

关于this

  • 函数中的this指向函数的调用者undefined(window)

下述代码中,报错原因是对象外的getAge无法识别函数体内的age

class Person{
    constructor(){
    this.age=12;
    }
    getAge()
    {
    return this.age;
    }
}
let p=new Person()
let GetAge=p.getAge
GetAge()
// Cannot read property 'age' of undefined
  • 用bind改进
class Person{
    constructor(){
    this.age=12;
    }
    getAge()
    {
    return this.age;
    }
}
let p=new Person()
let GetAge=p.getAge
GetAge.bind(p)//12
  • super()
class Stu extends Person{
    constructor(){
        super();//必须写在第一行
    this.age=13
    }
}

相关文章

网友评论

    本文标题:es6(class)

    本文链接:https://www.haomeiwen.com/subject/uxxzuxtx.html