mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-25 03:02:21 +00:00
Merge branch 'gh-pages' of github.com:ruanyf/es6tutorial into gh-pages
This commit is contained in:
commit
21f79e4346
@ -84,7 +84,7 @@ foo() // TypeError: Cannot read property 'x' of undefined
|
|||||||
下面是另一个对象的解构赋值默认值的例子。
|
下面是另一个对象的解构赋值默认值的例子。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
function fetch(url, { body = '', method = 'GET', headers = {} }){
|
function fetch(url, { body = '', method = 'GET', headers = {} }) {
|
||||||
console.log(method);
|
console.log(method);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,7 +100,7 @@ fetch('http://example.com')
|
|||||||
上面的写法不能省略第二个参数,如果结合函数参数的默认值,就可以省略第二个参数。这时,就出现了双重默认值。
|
上面的写法不能省略第二个参数,如果结合函数参数的默认值,就可以省略第二个参数。这时,就出现了双重默认值。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
function fetch(url, { method = 'GET' } = {}){
|
function fetch(url, { method = 'GET' } = {}) {
|
||||||
console.log(method);
|
console.log(method);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,7 +178,7 @@ f(1, undefined, 2) // [1, 5, 2]
|
|||||||
如果传入`undefined`,将触发该参数等于默认值,`null`则没有这个效果。
|
如果传入`undefined`,将触发该参数等于默认值,`null`则没有这个效果。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
function foo(x = 5, y = 6){
|
function foo(x = 5, y = 6) {
|
||||||
console.log(x, y);
|
console.log(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -764,7 +764,7 @@ var getTempItem = id => ({ id: id, name: "Temp" });
|
|||||||
const full = ({ first, last }) => first + ' ' + last;
|
const full = ({ first, last }) => first + ' ' + last;
|
||||||
|
|
||||||
// 等同于
|
// 等同于
|
||||||
function full( person ) {
|
function full(person) {
|
||||||
return person.first + ' ' + person.last;
|
return person.first + ' ' + person.last;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -832,14 +832,14 @@ headAndTail(1, 2, 3, 4, 5)
|
|||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
function foo() {
|
function foo() {
|
||||||
setTimeout( () => {
|
setTimeout(() => {
|
||||||
console.log('id:', this.id);
|
console.log('id:', this.id);
|
||||||
}, 100);
|
}, 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
var id = 21;
|
var id = 21;
|
||||||
|
|
||||||
foo.call( { id: 42 } );
|
foo.call({ id: 42 });
|
||||||
// id: 42
|
// id: 42
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -848,7 +848,7 @@ foo.call( { id: 42 } );
|
|||||||
箭头函数可以让`setTimeout`里面的`this`,绑定定义时所在的作用域,而不是指向运行时所在的作用域。下面是另一个例子。
|
箭头函数可以让`setTimeout`里面的`this`,绑定定义时所在的作用域,而不是指向运行时所在的作用域。下面是另一个例子。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
function Timer () {
|
function Timer() {
|
||||||
this.s1 = 0;
|
this.s1 = 0;
|
||||||
this.s2 = 0;
|
this.s2 = 0;
|
||||||
// 箭头函数
|
// 箭头函数
|
||||||
@ -895,9 +895,9 @@ var handler = {
|
|||||||
```javascript
|
```javascript
|
||||||
// ES6
|
// ES6
|
||||||
function foo() {
|
function foo() {
|
||||||
setTimeout( () => {
|
setTimeout(() => {
|
||||||
console.log('id:', this.id);
|
console.log('id:', this.id);
|
||||||
},100);
|
}, 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ES5
|
// ES5
|
||||||
@ -955,7 +955,7 @@ foo(2, 4, 6, 8)
|
|||||||
(function() {
|
(function() {
|
||||||
return [
|
return [
|
||||||
(() => this.x).bind({ x: 'inner' })()
|
(() => this.x).bind({ x: 'inner' })()
|
||||||
]
|
];
|
||||||
}).call({ x: 'outer' });
|
}).call({ x: 'outer' });
|
||||||
// ['outer']
|
// ['outer']
|
||||||
```
|
```
|
||||||
@ -1363,7 +1363,7 @@ function tco(f) {
|
|||||||
active = false;
|
active = false;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
var sum = tco(function(x, y) {
|
var sum = tco(function(x, y) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user