跳转至

面对对象编程

什么是面对对象

JavaScript,Java,c#。。。都是面对对象

类:模板

对象:具体的实例

在JavaScript是原型的概念

xxx.__proto__=XXX

Text Only
     var Student={
         name:"Lily",
         age:20,
         run:function (){
              console.log(this.name+" run...");
         }
     };
      var lucy={
          name:"lucy"
      } ;

      lucy.__proto__=Student;
Text Only
Student.run()
Lily run... basic.html:12:23
undefined
lucy.run()
lucy run... basic.html:12:23
undefined
Text Only
     function Student(name){
         this.name=name;
     }
     // 给student新增一个方法
     Student.prototype.hello=function (){
         alert("hello")
     }

class

ES6中引入class

JavaScript
     // ES6之后
     // 定义class
    class Student{
         constructor(name) {
             this.name=name;
         }
         hello(){
             alert("hello");
         }
    }
    var lucy=new Student("lucy");
     var jack=new Student("jack");

extends

Text Only
     // ES6之后
     // 定义class
    class Student{
         constructor(name) {
             this.name=name;
         }
         hello(){
             alert("hello");
         }
    }
    class Lucy extends Student{
         constructor(name,grade) {
             super(name);
             this.grade=grade;
         }
         myGrade(){
             alert("full mark");
         }
    }
    var lucy=new Lucy("lucy",100);
     var jack=new Student("jack");