JavaScript-the inheritance of object-oriented programming

Prototype inheritance (before Es6)

Prototype object (class)

Object: concrete instance

var student = {
    
    
  name : "shenming",
  age : 3,
  sex : '男',
  run:function(){
    
    
     console.log(this.name +"快跑");
  }
};


var xiaomimg = {
    
    
  name : "taoxian"
};

//原型继承 把xiaomimg的原型指向了student(原型对象),继承后xiaoming也可以调研student方法
xiaomimg.__proto__=student;

xiaomimg.run()
 taoxian快跑
student.run()
 shenming快跑

Class inheritance (new feature of Es6)

Define a class (including attributes, methods)

 //定义一个学生类
        class student{
    
    
           constructor(name){
    
    //构造函数
             this.name = "shenming"
           }
          hello(){
    
    
            alert("桃仙")
          }
        }

//定义学生类的变量(可以多个)
 var xiaoming = new student("xiaoming");
 var taoxian = new student("taoxian");
//类的使用
xiaoming.hello()
xiaoming.name
taoxian.hello()
taoxian.name

class inheritance

//定义一个学生类
        class Student{
    
    
           constructor(name){
    
    
             this.name = "shenming"
           }
          hello(){
    
    
            alert('hello')
          }
        }
        //class继承
        class XiaoStudent extends Student{
    
    
          constructor(name,grade){
    
    
             super(name);
             this.grade = grade;
          }
          myGrade(){
    
    
            alert('小学生')
          }
        }

        //定义学生类的变量,继承后,通过变量调用XiaoStudent 里的方法
        var xiaoming = new Student("xiaoming");
        var taoxian = new Student("taoxian");

The essence of class inheritance still points to the prototype (class), but the writing is easier to accept

Prototype chain

prototype property

In JavaScript, every function has a prototype property, which points to the prototype object of the function.

__ proto __ attributes

This is a property that every object (except null) will have, called proto, and this property will point to the prototype of the object.

Each object can have a prototype _proto_, this prototype can also have its own prototype, and so on to form a prototype chain. When looking for a specific property, we first go to this object to find it, if not, go to its prototype object, if it is still not there, then go to the prototype object of the prototype object to look for... This operation is delegated to the entire prototype chain Above, this is the prototype chain we are talking about.

Link : https://www.jianshu.com/p/08c07a953fa0

Guess you like

Origin blog.csdn.net/wpc2018/article/details/111657089