2-44钟静雯_day03
Day03作業 數據結構
概述 基本類型和引用類型
基本類型:String Boolean Number Null Undefined Symbol Bigint。
引用類型:Object(存儲屬性和屬性值) Function。
區別:
(內存存儲)基本類型存儲在棧(Stack)里面;引用類型在棧上存儲對堆上數據的引用(指針),在堆(Heap)上存儲引用類型本身的數據。
(清理方式)基本類型函數和方法執行完就清理內存;引用類型采用垃圾回收機制。
對象 存儲屬性和屬性值
定義對象:
Object構造器
對象字面量
數組 有序的值的列表
(與其他編程語言相比)區別:
數組的每個槽位可以存儲任意類型的數據。
動態大小 —— 影響性能。
定義數組:(聲明)
數組構造器
數組字面量
初始化:
解構數組:將數組中的元素取出來賦值給變量。
數組的屬性和方法:
length屬性:數組的長度。
const avengers = [‘美國隊長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
console.log(avengers.length);
// 通過設置length屬性改變數組元素(不可逆轉的改變,無副本)
avengers.length = 1;
console.log(avengers);// [ ‘美國隊長’ ]
avengers.length = 5;
console.log(avengers);// <1 empty item> Undefined
// 不改變數組元素:將length設為只讀屬性get。
pop()方法:刪掉數組中最后一個元素。
// 聲明數組
const avengers = [‘美國隊長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
// 數組維數縮小
console.log(avengers.pop());
console.log(avengers);
// delete運算符:元素個數沒變,刪掉元素變為Undefined
const avengers = [‘美國隊長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
delete avengers[0];
console.log(avengers);
pop()方法和delete運算符的區別:pop數組元素個數減少,delete運算符元素個數沒變。
push()方法:將新值添加到數組的末尾。
const avengers = [‘美國隊長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
avengers.push(‘蝙蝠俠’);
console.log(avengers);
shift()方法:刪除數組中的第一個元素。
const avengers = [‘美國隊長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
avengers.shift();
console.log(avengers);// [ ‘鋼鐵俠’, ‘雷神’, ‘綠巨人’ ]
console.log(avengers.shift());// 輸出刪除元素 鋼鐵俠
unshift()方法:將新值添加到數組的開頭。
const avengers = [‘美國隊長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
avengers.unshift(‘小超人’);
console.log(avengers);
console.log(avengers.unshift());
concat()方法:數組合并。
const avengers = [‘美國隊長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
const heroes = [‘蝙蝠俠’,‘神奇女俠’,‘閃電俠’,‘水行俠’];
const oArray = avengers.concat(heroes);// 賦值給變量,變量是新生成的數組。
console.log(avengers);
console.log(oArray);// 新建數組,進行合并。
// 擴展運算符:ES6新增。
const avengers = [‘美國隊長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
const heroes = [‘蝙蝠俠’,‘神奇女俠’,‘閃電俠’,‘水行俠’];
const oArray = […avengers,…heroes];// 元素扁平化
console.log(oArray);
join()方法:數組變成組合了數組所有元素的字符串。
const avengers = [‘美國隊長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
const a = avengers.join(&);// 變量
console.log(a);
slice()方法:從原始數組中切掉一片,從而創建一個子數組。
const avengers = [‘美國隊長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
const b = avengers.slice(2,3); // 從第二個元素開始到第三個元素結束(不包含)
console.log(b);// [ ‘雷神’ ]
console.log(avengers);
reverse()方法:反轉數組中元素的次序(永久性改變)。
const d = [‘a’,‘b’,‘c’,‘d’];
const e = d.reverse();// 賦值給變量
console.log(e,d);// [ ‘d’, ‘c’, ‘b’, ‘a’ ] [ ‘d’, ‘c’, ‘b’, ‘a’ ]
indexOf():檢測數組中是否包含一個特定值,如果找到了,就返回該值在數組中第一次出現的索引號,否則,就返回-1。
const avengers = [‘美國隊長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
const a1 = avengers.indexOf(‘美國隊長1’);
console.log(a1);// 0
includes():檢測數組中是否包含特定值,如果找到了,就返回true,否則就返回false。
const avengers = [‘美國隊長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
const a2 = avengers.includes(‘美國隊長2’);
console.log(a2);// false
多維數組:
作用:坐標系統(二維數組)以及復雜算法。
舉例:
const ma = [[1,2],[3,4]];
console.log(ma[0][0]);// 訪問元素
const summer = [‘Jun’,‘Jul’,‘Aug’];
const winter = [‘Dec’,‘Jan’,‘Feb’];
const nested = [summer,winter];
console.log(nested);// [ [ ‘Jun’, ‘Jul’, ‘Aug’ ], [ ‘Dec’, ‘Jan’, ‘Feb’ ] ]
// 擴展運算符扁平化為字符串
const summer = [‘Jun’,‘Jul’,‘Aug’];
const winter = [‘Dec’,‘Jan’,‘Feb’];
const flat = […summer,…winter];
console.log(flat);// [ ‘Jun’, ‘Jul’, ‘Aug’, ‘Dec’, ‘Jan’, ‘Feb’ ]
1
2
3
4
5
6
7
8
9
10
11
12
數組去重:(Set和數組的轉換)
const a = [1,2,12,1,2,3,4,5];// 循環讀取一個一個判斷
const b = new Set(a);
console.log(b);// Set { 1, 2, 12, 3, 4, 5 }
const c = […b];// 集合變數組
console.log?;
1
2
3
4
5
集合 唯一值的集合
定義集合:
構造函數,無字面量。
// 數字
const list = new Set();
list.add(1);
list.add(2).add(3).add(4).add(5);// 鏈式存儲
list.add(5);// 不能有重復值,直接忽略。
console.log(list);
// 初始化時用數組
const list = new Set([1,2,3,4,5]);
console.log(list);
// 字符
const c = new Set(‘Hello’);
console.log?;
// 鏈式操作,返回的一定是創建的本身
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
console.log(list4);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
屬性和方法:
size屬性:獲取集合中值的數目。(只讀屬性)
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
console.log(list4.size);
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
list4.size = 3;
console.log(list4);
1
2
3
4
5
6
has()方法:用于檢測一個值是否在集合中,返回true或者false。
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
console.log(list4.has(‘brown’));
1
2
delete()方法:從集合中刪除一個值。
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
list4.delete(‘the’);
console.log(list4);
1
2
3
clear()方法:刪掉集合中的所有值。
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
list4.clear();
console.log(list4);
1
2
3
Set和數組的轉換:數組去重也用到了。擴展運算符 Array.from() 方法
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
const oArray = […list4]; // 集合轉數組
console.log(oArray);
// 數組方法
const list4 = new Set().add(‘the’).add(‘quick’).add(‘brown’).add(‘fox’);
const oArray = Array.from(list4);
console.log(oArray);
1
2
3
4
5
6
7
8
集合 唯一值的集合
當對象添加到Set中時,只要Set存在,它們就會一直存儲在Set中,即使對對象的原始引用被刪除了依然如此。用技術術語來說,就是對象阻止被垃圾回收,而這會導致內存泄漏。
WeakSet 通過垃圾回收任何引用原始引用已經被刪掉的“死對象”,從而可以避免這種情況。
WeakSet 只能添加非基本類型數據,否則會拋出一個類型錯誤(TypeError)。
// Set強集合 內存泄漏
let array1 = [1, 2, 3];
let array2 = [3, 4, 5];
const strong = new Set().add(array1).add(array2); // 建集合,添加數組。
console.log(strong.has(array1));
array1 = null; // 刪除對原始對象的引用
array2 = null; // 原來數組設置為空
array3 = […strong][0]; // 1
array4 = […strong][1];
console.log(array3);
console.log(array4);// 數組還在
// WeakSet 避免內存泄漏
let array1 = [1,2,3];
let array2 = [3,4,5];
const weak = new WeakSet().add(array1).add(array2);// 扁平化
console.log(weak);
array1 = null; // 垃圾回收
array2 = null;
const array3 = […weak][0];
const array4 = […weak][1];
// 拋出異常
console.log(array3);
console.log(array4);// 類型錯誤,對象消失,不能引用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Map 存儲鍵值對列表
創建Map:
const romanNumerals = new Map();
romanNumerals.set(1,‘I’);
romanNumerals.set(2,‘II’).set(3,‘III’).set(4,‘IV’).set(5,‘V’);
console.log(romanNumerals);// Map { 1 => ‘I’, 2 => ‘II’, 3 => ‘III’, 4 => ‘IV’, 5 => ‘V’ }
1
2
3
4
方法和屬性:
size屬性:獲取鍵和值的數量。(只讀屬性)
const romanNumerals = new Map();
romanNumerals.set(1,‘I’);
romanNumerals.set(2,‘II’).set(3,‘III’).set(4,‘IV’).set(5,‘V’);
console.log(romanNumerals.size);// 5
1
2
3
4
get(key):通過鍵獲取值。
const romanNumerals = new Map();
romanNumerals.set(1,‘I’);
romanNumerals.set(2,‘II’).set(3,‘III’).set(4,‘IV’).set(5,‘V’);
console.log(romanNumerals.get(3));// III
1
2
3
4
has(key):檢測一個特定鍵是否在映射中。
const romanNumerals = new Map();
romanNumerals.set(1,‘I’);
romanNumerals.set(2,‘II’).set(3,‘III’).set(4,‘IV’).set(5,‘V’);
console.log(romanNumerals.has(5));// true
1
2
3
4
delete(key):從映射中刪除一個鍵值對。
const romanNumerals = new Map();
romanNumerals.set(1,‘I’);
romanNumerals.set(2,‘II’).set(3,‘III’).set(4,‘IV’).set(5,‘V’);
romanNumerals.delete(5);
console.log(romanNumerals);// Map { 1 => ‘I’, 2 => ‘II’, 3 => ‘III’, 4 => ‘IV’ }
1
2
3
4
5
clear():從映射中刪除所有鍵值對。
const romanNumerals = new Map();
romanNumerals.set(1,‘I’);
romanNumerals.set(2,‘II’).set(3,‘III’).set(4,‘IV’).set(5,‘V’);
romanNumerals.clear();
console.log(romanNumerals);
1
2
3
4
5
Map轉換為數組:
擴展運算符
Array.from() 方法
//map to array
const romanNumerals = new Map();
romanNumerals.set(1,‘I’).set(2,‘II’).set(3,‘III’).set(4,‘IV’).set(5,‘V’);// 鏈式
const oArray1 = […romanNumerals];
const oArray2 = Array.from(romanNumerals);
console.log(oArray1);
console.log(oArray2);
// [ [ 1, ‘I’ ], [ 2, ‘II’ ], [ 3, ‘III’ ], [ 4, ‘IV’ ], [ 5, ‘V’ ] ]
// [ [ 1, ‘I’ ], [ 2, ‘II’ ], [ 3, ‘III’ ], [ 4, ‘IV’ ], [ 5, ‘V’ ] ]
1
2
3
4
5
6
7
8
9
WeakMap
鍵不能是基本數據類型的值,并且在對原始對象的引用被刪除時,垃圾回收會自動刪除所有死條目。
// 內存泄漏
let array5 = [1, ‘I’];
let array6 = [2, ‘II’];
strong1 = new Map().set(array5).set(array6); // 創建映射,添加數組。
console.log(strong1.has(array5));
array5 = null;
array6 = null;
array7 = […strong1][0];
array8 = […strong1][1];
console.log(array7);// [ [ 1, ‘I’ ], undefined ]
console.log(array8);// [ [ 2, ‘II’ ], undefined ]
let array9 = [1, ‘I’];
let array10 = [2, ‘II’];
weak1 = new WeakMap().set(array9).set(array10); // 創建映射,添加數組。
console.log(weak1.has(array9));
array9 = null;
array10 = null;
array11 = […Weak1][0];
array12 = […Weak1][1];
console.log(array11);
console.log(array12);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
總結
數據類型
基礎類型:String Number Boolean Symbol undefined null。
引用類型:Object/Function。
對象
創建對象的兩種方式
構造器
let oStudent = new Object();
1
對象字面量
let oStudent = {
name: ‘xaaaa’,
age: 21
};
oStudent.name // 引用屬性
1
2
3
4
5
Array
1.創建數組的兩種方式
構造器
const oArray = new Array();
1
數組字面量
let person1 = {};
1
2.初始化
逐個賦值
const heroes = [];
heroes[0] = ‘蝙蝠俠’;
heroes[1] = ‘神奇女俠’;
1
2
3
數組字面量
const avengers = [‘美國隊長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
1
3.刪除數組元素
delete avengers[0];
1
4.解構數組
const [a,b,c] = [1,2,3];
console.log(a=${a},b=$,c=${c});
1
2
5.數組的屬性和方法
length屬性:數組的長度。
console.log(avengers.length);
1
pop()方法:刪掉數組中最后一個元素。
console.log(avengers.pop());
1
push()方法:將新值添加到數組的末尾。
avengers.push(‘蝙蝠俠’);
1
shift()方法:刪除數組中的第一個元素。
avengers.shift();
1
unshift()方法:將新值添加到數組的開頭。
avengers.unshift(‘小超人’);
1
concat()方法:數組合并。
const oArray = avengers.concat(heroes);
// 擴展運算符
const oArray = […avengers,…heroes];
1
2
3
join()方法:數組變成組合了數組所有元素的字符串。
const a = avengers.join(&);
1
slice()方法:從原始數組中切掉一片,從而創建一個子數組。
const b = avengers.slice(2,3);
1
splice()方法:從一個數組中刪除元素,然后將新元素插入在被刪除的元素的位置上。
const cc = avengers.splice(2,2,‘liwanling’,‘li’);
1
reverse()方法:反轉數組中元素的次序(永久性改變)。
const e = d.reverse();
1
sort()方法:對數組中的元素按字母順序進行排序(永久性改變)。
const g = f.sort();
1
indexOf():檢測數組中是否包含一個特定值,如果找到了,就返回該值在數組中第一次出現的索引號,否則,就返回-1。
const avengers = [‘美國隊長’,‘鋼鐵俠’,‘雷神’,‘綠巨人’];
const a1 = avengers.indexOf(‘美國隊長1’);
console.log(a1);// 0
1
2
3
includes():檢測數組中是否包含特定值,如果找到了,就返回true,否則就返回false。
const a2 = avengers.includes(‘美國隊長2’);
1
Set:沒有重復值
1.創建Set
構造器
let oSet = new Set();
1
2.添加元素
oSet.add(1).add(2)
1
3.Set的屬性和方法
size屬性:獲取集合中值的數目。
console.log(list4.size);
1
has()方法:用于檢測一個值是否在集合中,該方法會返回true或者false。
console.log(list4.has(‘brown’));
1
delete()方法:從集合中刪除一個值。
list4.delete(‘the’);
1
clear():刪掉集合中的所有值。
list4.clear();
1
4.Set和數組的轉換:擴展運算符 Array.from()方法
const oArray = […list4];
const oArray = Array.from(list4);
1
2
3
let oSet = new Set([1,2,3]); // 數組 迭代對象
1
5.WeakSet
const weak = new WeakSet().add(array1).add(array2);
console.log(weak);
array1 = null;
array2 = null;
const array3 = […weak][0];
const array4 = […weak][1];
1
2
3
4
5
6
Map
1.創建Map
const romanNumerals = new Map();
1
2.方法和屬性
size屬性:獲取鍵和值的數量。
console.log(romanNumerals.size);
1
get(key):通過鍵獲取值。
console.log(romanNumerals.get(3));
1
has(key):檢測一個特定鍵是否在映射中。
console.log(romanNumerals.has(5));
1
delete(key):從映射中刪除一個鍵值對。
romanNumerals.delete(5);
1
clear():從映射中刪除所有鍵值對。
console.log(romanNumerals);
1
3.Map轉換為數組:擴展運算符 Array.from() 方法
const oArray1 = […romanNumerals];
const oArray2 = Array.from(romanNumerals);
1
2
4.WeakMap
// 內存泄漏
strong1 = new Map().set(array5).set(array6); // 創建映射,添加數組。
weak1 = new WeakMap().set(array9).set(array10); // 創建映射,添加數組。
總結
以上是生活随笔為你收集整理的2-44钟静雯_day03的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 十年风雨路,中国MES市场亟呼健康的生态
- 下一篇: MySQL 分库拆表方案