js类中属性和方法不能重名

江枫思渺然 提交于 2020-08-13 08:40:01

最近撸react 看到了es6 extends的写法  想深入摸一摸 面向对象继承相关的东西 

老夫之前写php的时候 类内的属性和方法同名没事啊  子类或自动覆盖父类的啊 

然后!! js这里竟然报错了 究其原因是因为 js不支持方法和属性重名。。 

class parent{
	constructor(props) {
		this.repeat = 'parent'
	}
}
		
class child extends parent{
	constructor(){
		super()
		console.log(this.a)
	}
			
	repeat(){
		console.log(2)
	}
			
}
		
let c = new child();
c.repeat() //output: Uncaught TypeError: c.repeat is not a function
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!