1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-25 03:02:21 +00:00

edit docs/object

This commit is contained in:
Ruan Yifeng 2015-08-01 15:18:26 +08:00
parent 634ab7c5a2
commit 5071062c27
4 changed files with 90 additions and 4 deletions

View File

@ -458,7 +458,7 @@ A.prototype.__proto__ === Object.prototype // true
这种情况下A其实就是构造函数Object的复制A的实例就是Object的实例。
第二种特情况,不存在任何继承。
第二种特情况,不存在任何继承。
```javascript
class A {
@ -499,7 +499,7 @@ Object.getPrototypeOf(ColorPoint) === Point
### 实例的\_\_proto\_\_属性
父类实例和子类实例的\_\_proto\_\_属性指向是不一样的
子类实例的\_\_proto\_\_属性的\_\_proto\_\_属性指向父类实例的\_\_proto\_\_属性。也就是说子类的原型的原型是父类的原型
```javascript
var p1 = new Point(2, 3);

View File

@ -352,12 +352,45 @@ function (obj, proto) {
}
```
下面是一个例子。
```javascript
let proto = {};
let obj = { x: 10 };
Object.setPrototypeOf(obj, proto);
proto.y = 20;
proto.z = 40;
obj.x // 10
obj.y // 20
obj.z // 40
```
上面代码将proto对象设为obj对象的原型所以从obj对象可以读取proto对象的属性。
**3Object.getPrototypeOf()**
该方法与setPrototypeOf方法配套用于读取一个对象的prototype对象。
```javascript
Object.getPrototypeOf(obj)
Object.getPrototypeOf(obj);
```
下面是一个例子。
```javascript
function Rectangle() {
}
var rec = new Rectangle();
Object.getPrototypeOf(rec) === Rectangle.prototype
// true
Object.setPrototypeOf(rec, Object.prototype);
Object.getPrototypeOf(rec) === Rectangle.prototype
// false
```
## Symbol

View File

@ -463,7 +463,7 @@ escapeRegExp(str)
上面代码中str是一个正常字符串必须使用反斜杠对其中的特殊字符转义才能用来作为一个正则匹配的模式。
已经有[提议](https://esdiscuss.org/topic/regexp-escape)将这个需求标准化作为RegExp对象的静态方法[`RegExp.escape()`](https://github.com/benjamingr/RexExp.escape)放入ES7。
已经有[提议](https://esdiscuss.org/topic/regexp-escape)将这个需求标准化作为RegExp对象的静态方法[`RegExp.escape()`](https://github.com/benjamingr/RexExp.escape)放入ES7。2015年7月31日TC39认为这个方法有安全风险又不愿这个方法变得过于复杂没有同意将其列入ES7但这不失为一个真实的需求。
```javascript
RegExp.escape("The Quick Brown Fox");

View File

@ -460,3 +460,56 @@ const StyleGuide = {
export default StyleGuide;
```
## ESLint的使用
ESLint是一个语法规则和代码风格的检查工具可以用来保证写出语法正确、风格统一的代码。
首先安装ESLint。
```bash
$ npm i -g eslint
```
然后安装ES6插件和预设的Airbnb语法规则。
```bash
$ npm i -g babel-eslint eslint-config-airbnb
```
最后,在项目的根目录新建一个`.eslintrc`文件。
```javascript
{
"extends": "eslint-config-airbnb"
}
```
现在就可以检查当前项目的代码,是否符合规则。
假定`index.js`文件的代码如下。
```javascript
var unusued = 'I have no purpose!';
function greet() {
var message = 'Hello, World!';
alert(message);
}
greet();
```
使用ESLint检查这个文件。
```bash
$ eslint index.js
index.js
1:5 error unusued is defined but never used no-unused-vars
4:5 error Expected indentation of 2 characters but found 4 indent
5:5 error Expected indentation of 2 characters but found 4 indent
✖ 3 problems (3 errors, 0 warnings)
```
上面代码说明原文件有三个错误一个是定义了变量却没有使用另外两个是行首缩进为4个空格而不是规定的2个空格。