Class
Es5 generates instance objects through Constructors
function User(name, pass) { this.name = name this.pass = pass}User.prototype.log = function () { console.log(‘name = ‘ + this.name + ‘, pass = ‘ + this.pass)}var user1 = new User(‘xiaoming‘, ‘12356‘)user1.log()//name = xiaoming, pass = 12356
Es6 introduces the concept of class and constructor. When defining a "class" method, you do not need to add the function keyword before. Methods do not need to be separated by commas. If they are added, an error is returned.
class User { constructor(name, pass) { this.name = name this.pass = pass } log() { console.log(`name = ${this.name} , pass = ${this.pass}`) }}let user1 = new User(‘xiaoming‘, ‘12356‘)user1.log()//name = xiaoming , pass = 12356
Inheritance
Es5 inheritance
function VipUser(name, pass, level) { User.call(this, name, pass) this.level = level}VipUser.prototype = new User();VipUser.prototype.constructor = VipUserVipUser.prototype.logLevel = function () { console.log(‘level = ‘ + this.level)}var v1 = new VipUser(‘xiaoming‘, ‘123456‘, ‘5‘)v1.log()v1.logLevel()
Es6 inheritance (extends and super)
class VipUser extends User { constructor(name, pass, level) { super(name, pass) this.level = level } logLevel() { console.log(`level = ${this.level}`) }}let v1 = new VipUser(‘xiaoming‘, ‘1234‘, ‘5‘)
Es6 -- object-oriented