## 原型繼承
```
function People() {}
People.prototype.name="小明";
People.prototype.age = 20;
People.prototype.say = function() {
console.log("haha");
}
function Student(){}
Student.prototype = new People();
var student= new Student();
console.log(student.age);
```
## 原型繼承的優缺點
優點:
1. 實現簡單,可實現屬性和方法的共享
缺點:
1. 創建子類實例時,無法向父類構造函數傳參
2. 屬性包含引用類型時,更改子類會將所有的子類都修改
## 構造函數繼承
```
function People(name, age) {
this.name = name;
this.age = age;
}
function Student(name, age) {
People.call(this,name,age);
}
var student = new Student("xiaoming", 20);
console.log(student);
```
## 構造函數繼承的優缺點
優點:
1. 解決子類共享父類引用屬性的問題
2. 解決創建子類實例時,不可以向父類傳遞參數
3. 可以實現多繼承(call多個父類對象)
缺點:
1. 只能繼承父類的實例屬性和方法,不能繼承原型屬性/方法
2. 無法實現函數復用,每個子類都有父類實例函數的副本,影響性能
## 組合構造函數+原型
```
function People(name, age) {
this.name = name;
this.age = age;
// 僅在第一次調用的初始化
if (typeof this.say !== 'function') {
People.prototype.say = function() {
console.log("haha");
}
}
}
function Student(name, age) {
People.call(this,name,age);
}
Student.prototype = new People();
var student = new Student("xiaoming", 20);
console.log(student);
```
## 優缺點
優點:
1. 解決子類共享父類引用屬性的問題
2. 可以向父類傳遞參數
3. 函數可以復用
缺點:
1. 調用了兩次父類構造函數,生成了兩份實例(僅僅多消耗一點內存)
## 寄生組合模式
```
function obj(o) {
function F() {};
F.prototype = o;
return new F();
}
function People(name,age) {
this.name = name;
this.age = age;
// 僅在第一次調用的初始化
if (typeof this.say !== 'function') {
People.prototype.say = function() {
console.log("haha");
}
}
}
function Student(name, age) {
People.call(this,name,age);
}
var f= obj(People.prototype);
f.constructor = Student;
Student.prototype = f;
var student = new Student("xiaoming", 20);
student.say();
```
## 優缺點
優點: 最佳
缺點: 實現較為復雜
- 筆記內容來源
- 你不知道的JavaScript上
- vue
- 環境搭建
- node和npm安裝配置
- 安裝vue-cli并初始化vue項目
- 安裝配置elementUI
- vuex安裝配置
- axios安裝配置
- main.js
- vue基礎入門
- vue-router介紹
- vuex
- vue 原理學習源碼學習
- js正則處理v-bind和語法
- 雙向綁定
- 虛擬dom
- mvvm和render函數
- vue工作項目筆記
- elementUI 表格分頁多選記憶功能
- elementUI表格展開一行
- keepAlive
- vue整合ckeditor5
- this.$router.push 內打開新窗口
- java修改上傳圖片的權限
- 兼容ie11
- 生成二維碼
- base64圖片下載(兼容IE10)
- vue新手引導程序intro.js
- vue插件 devtools
- vue刷新當前頁面
- vue 錨點導航
- axios
- axios與springmvc
- vue-cli 3搭建vue
- git
- git常用命令
- 正則表達式
- 實例demo
- 1
- 新手引導頁
- 純css3從左顯示下劃線動畫導航菜單
- 純css3從中間顯示下劃線動畫導航菜單
- css顯示密碼
- 倒計時時鐘
- 星星評分
- 按鈕懸停效果
- 步驟條
- css動畫按鈕
- input標題獲得焦點上移
- css圖片放大
- css鏡像導航欄
- js
- 通信
- for in 和 for of
- 前端安全問題
- Promise
- 掘金冴羽學習筆記
- 模擬call
- 模擬bind
- 閉包
- 1 作用域
- 2 執行上下文棧
- 3 變量對象
- 4 作用域鏈
- 5 this
- 面向對象
- 基礎知識點
- 渲染機制
- 其他
- 判斷是否為數組
- http
- css
- 基礎知識
- css陰影