Skip to content

Commit a6ec140

Browse files
committed
edit object
1 parent 6fae5b1 commit a6ec140

File tree

4 files changed

+70
-35
lines changed

4 files changed

+70
-35
lines changed

docs/function.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,17 @@ foo( 2, 4, 6, 8 );
877877

878878
另外,由于箭头函数没有自己的`this`,所以当然也就不能用`call()``apply()``bind()`这些方法去改变`this`的指向。
879879

880+
```javascript
881+
(function() {
882+
return [
883+
(() => this.x).bind({ x: 'inner' })()
884+
]
885+
}).call({ x: 'outer' });
886+
// ['outer']
887+
```
888+
889+
上面代码中,箭头函数没有自己的`this`,所以`bind`方法无效,内部的`this`指向外部的`this`
890+
880891
长期以来,JavaScript语言的`this`对象一直是一个令人头痛的问题,在对象方法中使用`this`,必须非常小心。箭头函数”绑定”`this`,很大程度上解决了这个困扰。
881892

882893
### 嵌套的箭头函数
@@ -1152,7 +1163,6 @@ factorial(5) // 120
11521163
函数式编程有一个概念,叫做柯里化(currying),意思是将多参数的函数转换成单参数的形式。这里也可以使用柯里化。
11531164

11541165
```javascript
1155-
11561166
function currying(fn, n) {
11571167
return function (m) {
11581168
return fn.call(this, m, n);

docs/object.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ getPoint()
7676
// {x:1, y:10}
7777
```
7878

79-
模块输出变量,就非常合适使用简洁写法。
79+
CommonJS模块输出变量,就非常合适使用简洁写法。
8080

8181
```javascript
8282
var ms = {};
@@ -121,6 +121,22 @@ var cart = {
121121
}
122122
```
123123

124+
注意,简洁写法的属性名总是字符串,这会导致一些看上去比较奇怪的结果。
125+
126+
```javascript
127+
var obj = {
128+
class () {}
129+
};
130+
131+
// 等同于
132+
133+
var obj = {
134+
'class': function() {}
135+
};
136+
```
137+
138+
上面代码中,`class`是字符串,所以不会因为它属于关键字,而导致语法解析报错。
139+
124140
如果某个方法的值是一个Generator函数,前面需要加上星号。
125141

126142
```javascript

docs/reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
## 综合介绍
1010

11+
- Axel Rauschmayer, [Exploring ES6: Upgrade to the next version of JavaScript](http://exploringjs.com/es6/): ES6的专著,本书的许多代码实例来自该书
1112
- Sayanee Basu, [Use ECMAScript 6 Today](http://net.tutsplus.com/articles/news/ecmascript-6-today/)
1213
- Ariya Hidayat, [Toward Modern Web Apps with ECMAScript 6](http://www.sencha.com/blog/toward-modern-web-apps-with-ecmascript-6/)
1314
- Dale Schouten, [10 Ecmascript-6 tricks you can perform right now](http://html5hub.com/10-ecmascript-6-tricks-you-can-perform-right-now/)
@@ -25,6 +26,7 @@
2526
- Charles King, [The power of ECMAScript 6](http://charlesbking.com/power_of_es6/#/)
2627
- Benjamin De Cock, [Frontend Guidelines](https://github.com/bendc/frontend-guidelines): ES6最佳实践
2728
- Jani Hartikainen, [ES6: What are the benefits of the new features in practice?](http://codeutopia.net/blog/2015/01/06/es6-what-are-the-benefits-of-the-new-features-in-practice/)
29+
- kangax, [Javascript quiz. ES6 edition](http://perfectionkills.com/javascript-quiz-es6/): ES6小测试
2830

2931
## let和const
3032

docs/regex.md

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ new RegExp(/abc/ig, 'i').flags
2929

3030
ES6将这4个方法,在语言内部全部调用RegExp的实例方法,从而做到所有与正则相关的方法,全都定义在RegExp对象上。
3131

32-
- String.prototype.match 调用 RegExp.prototype[Symbol.match]
33-
- String.prototype.replace 调用 RegExp.prototype[Symbol.replace]
34-
- String.prototype.search 调用 RegExp.prototype[Symbol.search]
35-
- String.prototype.split 调用 RegExp.prototype[Symbol.split]
32+
- `String.prototype.match` 调用 `RegExp.prototype[Symbol.match]`
33+
- `String.prototype.replace` 调用 `RegExp.prototype[Symbol.replace]`
34+
- `String.prototype.search` 调用 `RegExp.prototype[Symbol.search]`
35+
- `String.prototype.split` 调用 `RegExp.prototype[Symbol.split]`
3636

3737
## u修饰符
3838

39-
ES6对正则表达式添加了u修饰符,含义为“Unicode模式”,用来正确处理大于`\uFFFF`的Unicode字符。也就是说,会正确处理四个字节的UTF-16编码。
39+
ES6对正则表达式添加了`u`修饰符,含义为“Unicode模式”,用来正确处理大于`\uFFFF`的Unicode字符。也就是说,会正确处理四个字节的UTF-16编码。
4040

4141
```javascript
4242
/^\uD83D/u.test('\uD83D\uDC2A')
@@ -45,7 +45,7 @@ ES6对正则表达式添加了u修饰符,含义为“Unicode模式”,用来
4545
// true
4646
```
4747

48-
上面代码中,“\uD83D\uDC2A”是一个四个字节的UTF-16编码,代表一个字符。但是,ES5不支持四个字节的UTF-16编码,会将其识别为两个字符,导致第二行代码结果为true。加了u修饰符以后,ES6就会识别其为一个字符,所以第一行代码结果为false
48+
上面代码中,“\uD83D\uDC2A”是一个四个字节的UTF-16编码,代表一个字符。但是,ES5不支持四个字节的UTF-16编码,会将其识别为两个字符,导致第二行代码结果为true。加了u修饰符以后,ES6就会识别其为一个字符,所以第一行代码结果为`false`
4949

5050
一旦加上u修饰符号,就会修改下面这些正则表达式的行为。
5151

@@ -127,11 +127,11 @@ codePointLength(s) // 2
127127
/[a-z]/iu.test('\u212A') // true
128128
```
129129

130-
上面代码中,不加u修饰符,就无法识别非规范的K字符。
130+
上面代码中,不加`u`修饰符,就无法识别非规范的K字符。
131131

132132
## y修饰符
133133

134-
除了u修饰符,ES6还为正则表达式添加了y修饰符,叫做“粘连”(sticky)修饰符。
134+
除了`u`修饰符,ES6还为正则表达式添加了`y`修饰符,叫做“粘连”(sticky)修饰符。
135135

136136
y修饰符的作用与g修饰符类似,也是全局匹配,后一次匹配都从上一次匹配成功的下一个位置开始。不同之处在于,g修饰符只要剩余位置中存在匹配就可,而y修饰符确保匹配必须从剩余的第一个位置开始,这也就是“粘连”的涵义。
137137

@@ -161,41 +161,50 @@ r.exec(s) // ["aa_"]
161161

162162
上面代码每次匹配,都是从剩余字符串的头部开始。
163163

164-
使用lastIndex属性,可以更好地说明y修饰符
164+
使用`lastIndex`属性,可以更好地说明`y`修饰符
165165

166166
```javascript
167167
const REGEX = /a/g;
168168

169-
REGEX.lastIndex = 2; // 指定从第三个位置y开始搜索
169+
// 指定从2号位置(y)开始匹配
170+
REGEX.lastIndex = 2;
171+
172+
// 匹配成功
170173
const match = REGEX.exec('xaya');
171174

172-
match.index
173-
// 3
174-
REGEX.lastIndex
175-
// 4
176-
REGEX.exec('xaxa')
177-
// null
175+
// 在3号位置匹配成功
176+
match.index // 3
177+
178+
// 下一次匹配从4号位开始
179+
REGEX.lastIndex // 4
180+
181+
// 4号位开始匹配失败
182+
REGEX.exec('xaxa') // null
178183
```
179184

180-
上面代码中,lastIndex属性指定每次搜索的开始位置,g修饰符从这个位置开始向后搜索,直到发现匹配为止。
185+
上面代码中,`lastIndex`属性指定每次搜索的开始位置,`g`修饰符从这个位置开始向后搜索,直到发现匹配为止。
181186

182-
y修饰符同样遵守lastIndex属性,但是要求必须在lastIndex指定的位置发现匹配
187+
y修饰符同样遵守`lastIndex`属性,但是要求必须在`lastIndex`指定的位置发现匹配
183188

184189
```javascript
185190
const REGEX = /a/y;
186191

187-
// 第三个位置y不匹配
192+
// 指定从2号位置开始匹配
188193
REGEX.lastIndex = 2;
189-
console.log(REGEX.exec('xaya')); // null
190194

191-
// 第四个位置出现匹配
195+
// 不是粘连,匹配失败
196+
REGEX.exec('xaya') // null
197+
198+
// 指定从3号位置开始匹配
192199
REGEX.lastIndex = 3;
200+
201+
// 3号位置是粘连,匹配成功
193202
const match = REGEX.exec('xaxa');
194203
match.index // 3
195204
REGEX.lastIndex // 4
196205
```
197206

198-
进一步说,y修饰符号隐含了头部匹配的标志ˆ
207+
进一步说,`y`修饰符号隐含了头部匹配的标志ˆ
199208

200209
```javascript
201210
/b/y.exec("aba")
@@ -235,11 +244,17 @@ const REGEX = /a/gy;
235244

236245
上面代码中,最后一个a因为不是出现下一次匹配的头部,所以不会被替换。
237246

238-
如果同时使用g修饰符和y修饰符,则y修饰符覆盖g修饰符。
239-
240-
y修饰符的主要作用,是从字符串提取token(词元),y修饰符确保了匹配之间不会有漏掉的字符。
247+
`y`修饰符的一个应用,是从字符串提取token(词元),`y`修饰符确保了匹配之间不会有漏掉的字符。
241248

242249
```javascript
250+
const TOKEN_Y = /\s*(\+|[0-9]+)\s*/y;
251+
const TOKEN_G = /\s*(\+|[0-9]+)\s*/g;
252+
253+
tokenize(TOKEN_Y, '3 + 4')
254+
// [ '3', '+', '4' ]
255+
tokenize(TOKEN_G, '3 + 4')
256+
// [ '3', '+', '4' ]
257+
243258
function tokenize(TOKEN_REGEX, str) {
244259
let result = [];
245260
let match;
@@ -248,14 +263,6 @@ function tokenize(TOKEN_REGEX, str) {
248263
}
249264
return result;
250265
}
251-
252-
const TOKEN_Y = /\s*(\+|[0-9]+)\s*/y;
253-
const TOKEN_G = /\s*(\+|[0-9]+)\s*/g;
254-
255-
tokenize(TOKEN_Y, '3 + 4')
256-
// [ '3', '+', '4' ]
257-
tokenize(TOKEN_G, '3 + 4')
258-
// [ '3', '+', '4' ]
259266
```
260267

261268
上面代码中,如果字符串里面没有非法字符,y修饰符与g修饰符的提取结果是一样的。但是,一旦出现非法字符,两者的行为就不一样了。

0 commit comments

Comments
 (0)