From 349e0fd0485c6f25f91962d39644eb3f2f303ef3 Mon Sep 17 00:00:00 2001 From: ruanyf Date: Thu, 15 Oct 2015 22:09:37 +0800 Subject: [PATCH] fix error --- docs/arraybuffer.md | 2 +- docs/decorator.md | 4 ++-- docs/function.md | 16 +++++++++++----- docs/generator.md | 4 ++-- docs/iterator.md | 28 ++++++++++++++-------------- docs/object.md | 2 +- docs/proxy.md | 10 +++++----- 7 files changed, 36 insertions(+), 30 deletions(-) diff --git a/docs/arraybuffer.md b/docs/arraybuffer.md index 8f4cac9..af77d9b 100644 --- a/docs/arraybuffer.md +++ b/docs/arraybuffer.md @@ -782,7 +782,7 @@ xhr.open('GET', someUrl); xhr.responseType = 'arraybuffer'; xhr.onload = function () { - var let arrayBuffer = xhr.response; + let arrayBuffer = xhr.response; // ··· }; diff --git a/docs/decorator.md b/docs/decorator.md index ece8be8..dbff61a 100644 --- a/docs/decorator.md +++ b/docs/decorator.md @@ -455,7 +455,7 @@ obj.foo() // "foo" ## Trait -Trait也是一种修饰器,功能与Mixin类型,但是提供更多功能,比如防止同名方法的冲突、排除混入某些方法、为混入的方法起别名等等。 +Trait也是一种修饰器,效果与Mixin类似,但是提供更多功能,比如防止同名方法的冲突、排除混入某些方法、为混入的方法起别名等等。 下面采用[traits-decorator](https://github.com/CocktailJS/traits-decorator)这个第三方模块作为例子。这个模块提供的traits修饰器,不仅可以接受对象,还可以接受ES6类作为参数。 @@ -478,7 +478,7 @@ obj.foo() // foo obj.bar() // bar ``` -上面代码中,通过traits修饰器,在MyClass类上面“混入”了TFoo类的foo方法和TBar对象的bar方法。 +上面代码中,通过traits修饰器,在`MyClass`类上面“混入”了`TFoo`类的`foo`方法和`TBar`对象的`bar`方法。 Trait不允许“混入”同名方法。 diff --git a/docs/function.md b/docs/function.md index 3338611..bc649fe 100644 --- a/docs/function.md +++ b/docs/function.md @@ -471,13 +471,19 @@ bar.name // "baz" bar.name // "baz" ``` -只有具名函数才有`name`这个属性,匿名函数是没有的。 +`Function`构造函数返回的函数实例,`name`属性的值为“anonymous”。 ```javascript -'name' in (function () {}) -// false -'name' in (() => {}) -// false +(new Function).name // "anonymous" +``` + +`bind`返回的函数,`name`属性值会加上“bound ”前缀。 + +```javascript +function foo() {}; +foo.bind({}).name // "bound foo" + +(function(){}).bind({}).name // "bound " ``` ## 箭头函数 diff --git a/docs/generator.md b/docs/generator.md index f25e909..52ed278 100644 --- a/docs/generator.md +++ b/docs/generator.md @@ -558,7 +558,7 @@ function log(generator) { var v; console.log('starting generator'); try { - g.next(); // { value: undefined, done: true } v = generator.next(); + v = generator.next(); // { value: undefined, done: true } console.log('第一次运行next方法', v); } catch (err) { console.log('捕捉错误', v); @@ -587,7 +587,7 @@ log(g()); // caller done ``` -上面代码一共三次运行next方法,第二次运行的时候会抛出错误,然后第三次运行的时候,Generator函数就已经结束了,不再执行下去了。 +上面代码一共三次运行`next`方法,第二次运行的时候会抛出错误,然后第三次运行的时候,Generator函数就已经结束了,不再执行下去了。 ## Generator.prototype.return() diff --git a/docs/iterator.md b/docs/iterator.md index 2568b0a..f197fc2 100644 --- a/docs/iterator.md +++ b/docs/iterator.md @@ -23,6 +23,12 @@ Iterator的遍历过程是这样的。 下面是一个模拟next方法返回值的例子。 ```javascript +var it = makeIterator(['a', 'b']); + +it.next() // { value: "a", done: false } +it.next() // { value: "b", done: false } +it.next() // { value: undefined, done: true } + function makeIterator(array){ var nextIndex = 0; return { @@ -33,12 +39,6 @@ function makeIterator(array){ } } } - -var it = makeIterator(['a', 'b']); - -it.next() // { value: "a", done: false } -it.next() // { value: "b", done: false } -it.next() // { value: undefined, done: true } ``` 上面代码定义了一个makeIterator函数,它是一个遍历器生成函数,作用就是返回一个遍历器对象。对数组`['a', 'b']`执行这个函数,就会返回该数组的遍历器对象(即指针对象)it。 @@ -52,6 +52,13 @@ next方法返回一个对象,表示当前数据成员的信息。这个对象 由于Iterator只是把接口规格加到数据结构之上,所以,遍历器与它所遍历的那个数据结构,实际上是分开的,完全可以写出没有对应数据结构的遍历器对象,或者说用遍历器对象模拟出数据结构。下面是一个无限运行的遍历器对象的例子。 ```javascript +var it = idMaker(); + +it.next().value // '0' +it.next().value // '1' +it.next().value // '2' +// ... + function idMaker(){ var index = 0; @@ -61,13 +68,6 @@ function idMaker(){ } } } - -var it = idMaker(); - -it.next().value // '0' -it.next().value // '1' -it.next().value // '2' -// ... ``` 上面的例子中,遍历器生成函数idMaker,返回一个遍历器对象(即指针对象)。但是并没有对应的数据结构,或者说,遍历器对象自己描述了一个数据结构出来。 @@ -488,7 +488,7 @@ for (a of arr) { Set和Map结构也原生具有Iterator接口,可以直接使用`for...of`循环。 ```javascript -var engines = Set(["Gecko", "Trident", "Webkit", "Webkit"]); +var engines = new Set(["Gecko", "Trident", "Webkit", "Webkit"]); for (var e of engines) { console.log(e); } diff --git a/docs/object.md b/docs/object.md index 03792de..e9a7ee1 100644 --- a/docs/object.md +++ b/docs/object.md @@ -481,7 +481,7 @@ var obj = Object.create(someOtherObj); obj.method = function() { ... } ``` -该属性没有写入ES6的正文,而是写入了附录,原因是`__proto__`前后的双引号,说明它本质上是一个内部属性,而不是一个正式的对外的API,只是由于浏览器广泛支持,才被加入了ES6。标准明确规定,只有浏览器必须部署这个属性,其他运行环境不一定需要部署,而且新的代码最好认为这个属性是不存在的。因此,无论从语义的角度,还是从兼容性的角度,都不要使用这个属性,而是使用下面的`Object.setPrototypeOf()`(写操作)、`Object.getPrototypeOf()`(读操作)、`Object.create()`(生成操作)代替。 +该属性没有写入ES6的正文,而是写入了附录,原因是`__proto__`前后的双下划线,说明它本质上是一个内部属性,而不是一个正式的对外的API,只是由于浏览器广泛支持,才被加入了ES6。标准明确规定,只有浏览器必须部署这个属性,其他运行环境不一定需要部署,而且新的代码最好认为这个属性是不存在的。因此,无论从语义的角度,还是从兼容性的角度,都不要使用这个属性,而是使用下面的`Object.setPrototypeOf()`(写操作)、`Object.getPrototypeOf()`(读操作)、`Object.create()`(生成操作)代替。 在实现上,`__proto__`调用的是`Object.prototype.__proto__`,具体实现如下。 diff --git a/docs/proxy.md b/docs/proxy.md index 30a753e..721539f 100644 --- a/docs/proxy.md +++ b/docs/proxy.md @@ -340,10 +340,10 @@ var twice = { apply (target, ctx, args) { return Reflect.apply(...arguments) * 2; } -} +}; function sum (left, right) { return left + right; -} +}; var proxy = new Proxy(sum, twice); proxy(1, 2) // 6 proxy.call(null, 5, 6) // 22 @@ -370,10 +370,10 @@ var handler = { } return key in target; } -} +}; var target = { _prop: 'foo', prop: 'foo' }; -'_prop' in proxy -// false +var proxy = new Proxy(target, handler); +'_prop' in proxy // false ``` 上面代码中,如果原对象的属性名的第一个字符是下划线,`proxy.has`就会返回`false`,从而不会被`in`运算符发现。