1 原型对象
// 原型对象
var Student={
name:'student',
age:'3',
run:function () {
console.log(this.name+' run');
},
}
var xiaoming={
name:'xiaoming',
}
// 小明的原型是 Student 类似于Java中的父类
xiaoming.__proto__=Student;
xiaoming.run();
2 class继承
class关键字是在ES6引入的
// 定义一个学生的类
class Student{
// 构造器
constructor(name){
this.name=name;
}
hello=function () {
console.log(this.name+' hello');
}
}
// 继承
class xiaoStudent extends Student{
constructor(name,age){
super(name);
this.age=age;
}
hello=function () {
console.log(this.name+this.age+' hello');
}
}
var xiaoming=new Student('xiaoming');
xiaoming.hello();
var xiaohong=new xiaoStudent('xiaohong',3);
xiaohong.hello();
xiaoming hello
xiaohong3 hello