Prototype
class
-constructor 属性和方法
class名称以大写字母开头
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Contect-Type" charset="utf-8">
<title>classdemo</title>
</head>
<body>
</body>
<p>classdemo</p>
<script>
class Student{
constructor(name,number){
this.name =name
this.number =number
}
sayHi(){
console.log(
`name ${this.name},number ${this.number}`
)
}
}
//通过类声明对象实例
const james = new Student('james',100);
console.log(james.name);
console.log(james.number);
james.sayHi();
const tom = new Student('tom',101);
console.log(tom.name);
console.log(tom.number);
tom.sayHi();
</script>
</html>
继承
-extends
-super
扩展或重写方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Contect-Type" charset="utf-8">
<title>extendsdemo</title>
</head>
<body>
</body>
<p>extendsdemo</p>
<script>
class People{
constructor(name){
this.name=name+'sir'
}
eat(){
console.log(`${this.name} eat some food`)
}
}
//子类
class Teacher extends People{
constructor(name,major){
super(name)
this.major=major
}
teach(){
console.log(`${this.name},teach ${this.major}`)
}
}
class Student extends People{
constructor(name,number){
super(name)
this.number=number
}
sayHi(){
console.log(`name ${this.name},number ${this.number}`)
}
}
const james = new Student('james',100);
console.log(james.name);
console.log(james.number);
james.sayHi();
james.eat();
const wang = new Teacher('wang','math');
console.log(wang.name);
console.log(wang.major);
wang.teach();
wang.eat();
</script>
</html>