luhao‘s blog


  • 首页

  • 标签

  • 分类

  • 归档

Set and Map

发表于 2019-03-28 | 分类于 ES6 notes

Set

Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用。

NaN 是与 NaN 相等的(虽然 NaN !== NaN),剩下所有其它的值是根据 === 运算符的结果判断是否相等。

基本集合操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
let setA = new Set([1, 2, 3, 4]),
setB = new Set([2, 3]),
setC = new Set([3, 4, 5, 6]);

//包含
function isSuperSet(set, subset) {
for (let elem of subset) {
if (!set.has(elem)) {
return false
}
}
return true;
}

//交叉
function intersection(setA, setB) {
let _intersection = new Set();
for (let elem of setB) {
if (setA.has(elem)) {
_intersection.add(elem);
}
}

return _intersection;
}

//联合
function union(setA, setB) {
let _union = new Set(setA);
for (let elem of setB) {
_union.add(elem);
}

return _union;
}

//不是很完善
function difference(setA, setB) {
let _difference = new Set(setA);
for (let elem of setB) {
_difference.delete(elem);
}

return _difference;
}

console.log(isSuperSet(setA, setB)); // => true
console.log(union(setA, setC)); // => Set [1, 2, 3, 4, 5, 6]
console.log(intersection(setA, setC)); // => Set [3, 4]
console.log(difference(setA, setC)); // => Set [1, 2]
阅读全文 »

数组拓展

发表于 2019-03-26 | 分类于 ES6 notes

调用 new Array()构造器时, 根据传入参数的类型与数量的不同,实际上会导致一些不同的结果。

1
2
3
4
items = new Array(3, "2");
console.log(items.length); // 2
console.log(items[0]); // 3
console.log(items[1]); // "2"

如果使用多个参数(无
论是否为数值类型)来调用,这些参数也会成为目标数组的项。

1
2
let items = Array.of("2");
//items: ['2']

向函数传递参数,使用 Array.of() 而非 Array 构
造器能够确保行为一致。

1
2
3
4
function createArray(arrayCreator, value) {
return arrayCreator(value);
}
let items = createArray(Array.of, value);
阅读全文 »

字符串的拓展

发表于 2019-03-26 | 分类于 ES6 notes

字符的 Unicode 表示法

JavaScript 允许采用\uxxxx形式表示一个字符,其中xxxx表示字符的 Unicode 码点。
但是,这种表示法只限于码点在\u0000~\uFFFF之间的字符。超出这个范围的字符,必须用两个双字节的形式表示。

1
2
3
4
5
"\uD842\uDFB7"
// "𠮷"

"\u20BB7"
// "₻7"

加入大括号

1
2
"\u{20BB7}"
// "𠮷"

JavaScript 共有 6 种方法可以表示一个字符

1
2
3
4
5
'\z' === 'z'  // true
'\172' === 'z' // true
'\x7A' === 'z' // true
'\u007A' === 'z' // true
'\u{7A}' === 'z' // true

codePointAt()

1
2
3
4
5
6
7
var s = "𠮷";

s.length // 2
s.charAt(0) // '�'
s.charAt(1) // '�'
s.charCodeAt(0) // 55362
s.charCodeAt(1) // 57271
阅读全文 »

解构赋值

发表于 2019-03-25 | 分类于 ES6 notes

对象解构

必须用圆括号包裹解构赋值语句,这是因为暴露的花括号会被解析为代码块语
句,而块语句不允许在赋值操作符(即等号)左侧出现。圆括号标示了内部的花括号并不是
块语句、而应该被解释为表达式,从而完成赋值操作。

1
2
3
4
5
6
7
8
9
10
11
let node = {
type: "Identifier",
name: "foo"
},
type = "Literal",
name = 5;
// 使用解构来分配不同的值
({ type, name } = node);

console.log(type); // "Identifier"
console.log(name); // "foo"

顺序不同也没事

也可以结合冒号和等号一起使用

1
let {width: w = 100, height: h = 200, title} = options;

实际上是这样 let { foo: foo, bar: bar } = { foo: “aaa”, bar: “bbb” };

阅读全文 »

let and const

发表于 2019-03-25 | 分类于 ES6 notes

const 会阻止对于变量绑定与变量自身值的修改。并不会阻止对变量成员的修改

1
2
3
4
5
6
7
8
9
const person = {
name: "Nicholas"
};
// 工作正常
person.name = "Greg";
// 抛出错误
person = {
name: "Greg"
};

暂时性死区

使用 let 或 const 声明的变量,在达到声明位置之前都是无法访问的,试图访问会导致一
个引用错误,即便所用的操作通常是安全的,例如使用 typeof 运算符

1
2
3
4
if (condition) {
console.log(typeof value); // 引用错误
let value = "blue";
}
1
2
3
4
5
var x = 10;
if (true) {
x = 20; // ReferenceError
let x;
}
阅读全文 »

渡一笔记

发表于 2019-02-28 | 分类于 JS

预编译

  1. 创建AO对象
  2. 找形参和变量声明, 将变量和形参名作为AO属性名,值为undefined
  3. 将实参值和形参值统一
  4. 在函数体里面找函数声明,值赋予函数体
1
2
3
4
5
6
7
8
9
10
11
12
13
function test(a, b) {
console.log(a);
c = 0;
var c;
a = 3;
b = 2;
console.log(b);
function b () {}
function d () {}
console.log(b);
}
test(1)

阅读全文 »

1037378313@qq.com

记录成长道路上的点点滴滴

6 日志
2 分类
2 标签
© 2019 1037378313@qq.com
由 Hexo 强力驱动
|
主题 — NexT.Gemini v5.1.4