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__