javascript 面向对象

// 三要素
// 继承,子类继承父类
// 封装,数据的权限和保密
// 多态,同一接口不同实现


// 三要素-继承
// person是父类,公共的,不仅仅服务于Student
// 继承可将公共方法抽离出来,提高复用,减少冗余
// class Person{
//    constructor(name,age){
//      this.name = name
//      this.age = age
//   }
//    eat(){
//      alert(`${this.name} eat something`)
//   }
//   speak(){
//      alert(`My name is ${this.name},age ${this.age}`)
//    }
// }

// extends继承什么 子类继承父类
// class Student extends Person{
//    constructor(name,age,number){
//      super(name,age) //转到父类
//     this.number = number
//    }
//    study(){
//      alert(`${this.name} study`)
//   }
// }

// let xiaoming = new Student("xiaoming",10,"A1")
// xiaoming.study()
// alert(xiaoming.number)
// xiaoming.eat()

// let xiaohong = new Student("xiaohong",10,"A2")
// xiaohong.study()
// xiaohong.speak()


// 三要素-封装
// public 完全开放
// protected 对子类开放
// private 对自己开放

// 减少耦合,不该外露的不外露
// 利于数据,接口的权限管理
// es6目前不支持 ,一般认为_开头的属性是private

// typescript

// 这段代码在typescript中可以执行
// class Person{
//   name
//    age
//    protected weight //定义protected属性
//    constructor(name,age){
//      this.name = name
//      this.age = age
//      this.weight = 120
//    }
//    eat(){
//      alert(`${this.name} eat something`)
//   }
//    speak(){
//      alert(`My name is ${this.name},age ${this.age}`)
//    }
// }


// class Student extends Person{
//   number
//    private girlfriend //定义private属性
//    constructor(name,age,number){
//      super(name,age) //转到父类
//      this.number = number
//      this.girlfriend = 'xiaoli'
//    }
//    study(){
//      alert(`${this.name} study`)
//    }
//    getweight(){
//      alert(`${this.weight}`)
//    }
// }

// let xiaoming = new Student("xiaoming",10,"A1")
// xiaoming.getweight()
// alert(xiaoming.girlfriend)


// 三要素-多态
// 同一接口,不同表现
// js应用极少
// 需要结合java等语言的接口,重写,重载等功能
// 保存子类分开放性和灵活性
// 面向接口编程
// (js引用极少,了解即可)

class Person{
  constructor(name){
    this.name = name
  }
  saySomething(){

  }
}

class A extends Person{
  constructor(name){
    super(name)
  }
  saySomething(){
    alert("I'am A")
  }
}

class B extends Person{
  constructor(name){
    super(name)
  }
  saySomething(){
    alert("I'am B")
  }
}

// let a = new A('a')
// a.saySomething()
// let b = new B('b')
// b.saySomething()


class jQuery{
  constructor(seletor){
    //.....
    let slice = Array.prototype.slice
    let dom = slice.call(document.querySelectorAll(seletor))
    let len = dom ? dom.length : 0
    for(let i =0;i<len;i++){
      this[i] = dom[i]
    }
    this.length = len
    this.seletor = seletor || ''
  }
  append(node){
    //.....
  }
  html(data){
    //
  }
  addClass(name){
    //
  }

}

window.$ = function(seletor){
  //工厂模式
  return new jQuery(seletor)
}

var $p = $('p')
console.log($p)
console.log($p.addClass)

// 为何使用面向对象?

// 程序执行:顺序,判断,循环--结构化
// 面向对象--数据结构化
// 对于计算机,结构化的才是最简单的
// 编程应该 简单&抽象

原文地址:https://www.cnblogs.com/sklhtml/p/9485800.html