相同的一个自定义构造函数两种写法的差异。

构造函数

function Wumao (name, age) {
    this.name = name,
    this.age = age
}
var a = new Wumao
a.protype.sayHello = function() {
    console.log('hello world')
}

class类

class Wumao {
    constructor (name, age) {
        this.name = name
        this.age = age
    }

    getName () {
        console.log('hello world')
    }
}

Class类是js构造函数的一个语法糖,具体使用可以看下《es6标准入门》

Last modification:January 14th, 2020 at 10:51 pm
If you think my article is useful to you, please feel free to appreciate