沒有連接的是尚未寫的,計劃要寫的,歡迎閱讀交流~
前端設計模式(0)面向對象&&設計原則
前端設計模式(1)--工廠模式
前端設計模式(2)--單例模式
前端設計模式(3)--適配器模式
前端設計模式(4)--裝飾器模式
前端設計模式(5)--代理模式
前端設計模式(6)--外觀模式&&觀察者模式
前端設計模式(7)--狀態和策略模式
前端設計模式(8)--原型模式
...前端
把客觀對象抽象成屬性數據和對數據的相關操做,把內部細節和不想關的信息隱藏起來,把同一個類型的客觀對象的屬性數據和操做綁定在一塊兒,封裝成類,而且容許分紅不一樣層次進行抽象,經過繼承實現屬性和操做的共享,來,前端設計模式咱們從面向對象開始。編程
/**
* 類 對象(實例)
* 父類Animal是公共的
*/
class Animal {
constructor (name) {
this.name = name
}
eat () {
console.log(`${this.name}eat`)
}
}
let animal = new Animal('動物')
animal.eat()
/**
* 繼承
* 子類繼承父類
* 繼承能夠把公共的方法抽離出來,提升複用,減小冗餘
*/
class Animal {
constructor (name) {
this.name = name
}
eat () {
console.log(`${this.name}eat`)
}
}
class Cat extends Animal {
constructor (myname, age) {
super(myname)
this.age = age
}
speak () {
console.log(`${this.name}:喵喵~`)
}
}
let cat = new Cat('小花貓', 0)
cat.eat ()
cat.speak ()
複製代碼
這裏說明一下, 把數據封裝起來 減小耦合,不應外部訪問的不要讓外部訪問 利於數據的接口權限管理 ES6 目前不支持,通常認爲_開頭的都會私有的,不要使用,後面講的會使用ts,有不瞭解的童鞋能夠去官網看看,2分鐘入門json
class Animal2 {
public name // 公共的,類內或者類外均可以訪問,好比:你的名字誰均可以知道
protected age // 收保護的本身和本身的子類能夠訪問,好比:女性的年齡
private monney // 只有本身能夠知道哦,私房錢嘛
constructor (name, age, monney) {
this.name = name
this.age = age
this.monney = monney
}
}
class Person2 extends Animal2 {
private card;
constructor(name,age,monney,card) {
super(name, age, monney)
this.card=card;
}
getName() {
console.log(this.name);
}
getAge() {
console.log(this.age);
}
getWeight() {
console.log(this.monney); // [ts] 屬性「monney」爲私有屬性,只能在類「Animal2」中
}
}
let person = new Person2('dafei', 18, '100000', '金卡')
複製代碼
同一個接口能夠不一樣實現設計模式
保持子類的開放性和靈活性bash
面向接口編程post
class Animal {
public name;
protected age;
private weight;
constructor(name,age,weight) {
this.name=name;
this.age=age;
this.weight=weight;
}
}
class Person extends Animal {
private money;
constructor(name,age,weight,money) {
super(name,age,weight);
this.money=money;
}
speak() {
console.log('你好!');
}
}
class Dog extends Animal {
private money;
constructor(name,age,weight) {
super(name,age,weight);
}
speak() {
console.log('汪汪汪!');
}
}
let p=new Person('dafei',10,10,10);
p.speak();
let d=new Dog('dafei',10,10);
d.speak();
複製代碼
function parseJSON(response) {
return response.json();
}
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
}
const error = new Error(response.statusText);
error.response = response;
throw error;
}
export default function request(url, options) {
return fetch(url, options)
.then(checkStatus)
.then(parseJSON)
.then(data=>{data})
.catch(err => ({ err }));
}
複製代碼
還有L 里氏替換原則、I 接口隔離原則、D 依賴倒置原則,JS使用比較少。學習
下一遍咱們開始來一塊兒學習,工廠模式fetch