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:
parent
634ab7c5a2
commit
5071062c27
@ -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);
|
||||
|
@ -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对象的属性。
|
||||
|
||||
**(3)Object.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
|
||||
|
@ -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");
|
||||
|
@ -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个空格。
|
||||
|
Loading…
x
Reference in New Issue
Block a user