面向对象编程(JavaScript)

suaxi
2020-12-09 / 0 评论 / 217 阅读 / 正在检测是否收录...
原型对象
var Student = {
    name: "孙笑川",
    age: 33,
    run:function () {
        console.log(this.name + "run ...")
    }
}
        
var sun = {
    name: "sun"
}

//原型对象
sun.__proto__ = Student;
function Student(name){
    this.name = name;
}

//给Student新增一个方法
Student.prototype.hello = function () {
    alert("hello")
}
class 继承

classs关键字在ES6引入

1、定义一个类

class Student{
    constructor(name){
        this.name = name;
    }
    
    hello(){
        alert("hello")
    }
}

var sun = new Student("sun");
sun.hello()

2、继承

class Student{
    constructor(name){
    this.name = name;
    }

    hello(){
    alert("hello")
    }
}

class highStudent extends Student{
    constructor(name,score){
    super(name);
    this.score = score;
}

    myScore(){
        alert("100分!")
    }
}

var sun = new highStudent("sun",100);

本质:查看对象原型

class继承.png

原型链

原型链.png

0

评论 (0)

取消