linche0859
7/18/2019 - 5:56 AM

鏈式

物件鏈式

const people = function(name = String, age = Number) {
  this.name = name
  this.age = age
  this.sayName = function() {
    console.log(`My name is ${this.name}`)
    // 記得要return this
    return this
  }
  this.sayAge = function() {
    console.log(`My age is ${this.age}`)
    // 記得要return this
    return this
  }
}

const man = new people('lin', 27)
console.log(man)
man.sayName().sayAge()
結果
people { name: 'lin', age: 27, sayName: [Function], sayAge: [Function] }
My name is lin
My age is 27