es6中类可以调用自己的方法吗?

我想在初始化对象时,将this.detailImgUrls用concatImgUrls()初始化一下,请问应该用下面那种方法?

function concatImgUrls() {
    return 'haha';
}

export default class Car{
    constructor(data){
        this.address = data.address;
        this.displacement = data.displacement + 'L';
        
        this.detailImgUrls = concatImgUrls();

    }
}
export default class Car{
    constructor(data){
        this.address = data.address;
        this.displacement = data.displacement + 'L';
        this.detailImgUrls = this.concatImgUrls();

    }

    concatImgUrls() {
        return 'haha';
    }


}

两种我试了一下,都可以,但按理说第二种情况中,对象方法是给实例化出来的对象使用的,而不是在类中使用的啊。

阅读 3.2k
1 个回答

es6 是如何实现, 原型链, 原因就知道了

使用是自己定的, 每种强类型语言也都没有限定死

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进