代码如下
/* A类,方法写在constructor里面 */
class A{
constructor(){
this.show = function (){
console.log( 'A show' )
}
}
}
const a = new A()
a.show() // => A show
/* B类,方法写在constructor外面 */
class B{
constructor(){
}
show(){
console.log( 'B show' )
}
}
const b = new B()
b.show() // => B show
提问点
A类的show方法和B类的show方法调用结果都一样,那么请问写在constructor里和外面的区别是?
B的show方法是在prototype上