Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
40 lines (32 sloc) 662 Bytes
title tags
JavaScript的语言陷阱
javascript

一个例子,猜一下会不会Throw Error

class Father {
  constructor() {
    if (arguments.length === 0) {
      throw Error('1')
    } else {
      this.foo = arguments[0]
    }
  }
}

class Son extends Father {
  constructor(first, second) {
    super(first)
  }
}

const son = new Son()

没有

为什么呢,因为 Son 类型虽然不存在 first 参数,但是它是 undefined,所以它变成了参数传递给了父类,于是 this.foo = undefined

正确方法

class Son extends Father {
  constructor(first, second) {
    super(...arguments)
  }
}
You can’t perform that action at this time.