JavaScript学习笔记十二 —— 面向对象继承
参考教程B站狂神https://www.bilibili.com/video/BV1JJ41177di
原型指向
__proto__
var user = {
name: "ylw",
age: 3,
run: function () {
console.log(this.name + "run...");
}
}
var xioaming = {
name: "xiaoming"
};
// 小明的原型是user,这样写xiaoming就有user里的属性和方法了
xioaming.__proto__ = user;
function Student(name) {
this.name = name;
//在内部添加方法
hello: function f() {
}
}
//在外部添加方法
Student.prototype.hello = function () {
}
class继承
在es6引入的
定义一个类,属性,方法
//es6 之后
//定义一个学生的类
class Student{
//构造器
constructor(name){
this.name = name;
}
//添加方法
hello(){
}
}
//new对象
var xiaoming = new Student("xiaoming");
继承
//es6 之后
//定义一个学生的类
class Student{
//构造器
constructor(name){
this.name = name;
}
//添加方法
hello(){
}
}
//继承
class XiaoStudent extends Student{
constructor(name,grade) {
super(name);
this.grade = grade;
}
myGrade(){
alert("我是一名小学生");
}
}
//new对象
var xiaoming = new Student("xiaoming");
var xioahong = new XiaoStudent("xiaohong",1);
原型链
__proto__