# 類
> 類(class)本質上是 es5 構造函數的另一種寫法. </br>
> </br>
> **注意:** </br>
> 1.class 定義的對象,不用 **new 關鍵字**聲明是無法運行的,和函數加 () 是有些區別的.
> 2.class 不存在變量提升.
> 3.class 內部使用的是嚴格模式.
```javascript
//es5
var Monkey = function(name, age) {
this.name = name;
this.age = age;
this.lol = function() {
console.log("i'm laughing~");
};
};
var monkey1 = new Monkey('neer', 2);
monkey1.lol();
console.log("i'm" + monkey1.name + ', ' + monkey1.age + 'years old');
//es6
class Monkey {
constructor(name, age) {
this.name = name;
this.age = age;
}
lol() {
console.log("i'm laughing~");
}
}
let monkey1 = new Monkey('neer', 2);
monkey1.lol();
console.log(`i'm ${monkey1.name}, ${monkey1.age} years old`);
//子類必須在constructor方法中調用super方法,否則新建實例時會報錯。這是因為子類沒有自己的this對象,而是繼承父類的this對象,然后對其進行加工。如果不調用super方法,子類就得不到this對象。
// 類的繼承
class Person {
constructor(name, gender) {
this.name = name;
this.gender = gender;
}
introduction() {
console.log(`my name is ${this.name} and i'm a ${this.gender}`);
}
}
class Son extends Person {
constructor(name, gender, age) {
super(name, gender);
this.age = age;
}
call() {
console.log(
`i'm ${this.name}'s son, my father is ${this.gender},i was ${
this.age
} years old `
);
}
}
```
- 01.let-const
- 02.對象數組解構&賦值
- 03.字符串擴展,數值擴展,數組擴展
- 04.數組擴展
- 05.對象擴展
- 06.06.Symbol原始數據類型
- 07.set數據結構
- 08.map數據結構
- 09.proxy與Reflect
- 10.類
- 11.Promise
- 12.Iterator(迭代器)
- 13.Generator(生成器)
- 14.module與模塊化
- 15.es6學習總結
- 記錄- Vue拖拽實例
- 記錄-git使用天坑之分支切換
- node -- session & cookie & localStorge
- 18.12關于前端戰略技術儲備與問題反饋
- Vue組件通信方式總結以及遇到的問題
- 01.版本回溯以及文件修改
- 02.遠端控制
- 03.分支管理
- node 入門 留言板
- nodejs模塊與 commonjs 規范
- 19年技術發展規劃
- JS錯誤處理 -> 提升程序健壯性
- Git 基本使用
- 18年年終總結