Skip to content

Commit

Permalink
docs(proxy): construct方法
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanyf committed May 29, 2016
1 parent f7494bc commit 18b2f80
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
17 changes: 15 additions & 2 deletions docs/number.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ ES5可以通过下面的代码,部署`Number.isFinite`方法。
})(this);
```

`Number.isNaN()`用来检查一个值是否为NaN
`Number.isNaN()`用来检查一个值是否为`NaN`

```javascript
Number.isNaN(NaN) // true
Expand Down Expand Up @@ -236,6 +236,8 @@ Number.MIN_SAFE_INTEGER === -9007199254740991
// true
```

上面代码中,可以看到JavaScript能够精确表示的极限。

`Number.isSafeInteger()`则是用来判断一个整数是否落在这个范围之内。

```javascript
Expand All @@ -256,7 +258,18 @@ Number.isSafeInteger(Number.MAX_SAFE_INTEGER) // true
Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1) // false
```

注意,验证运算结果是否落在安全整数的范围时,不要只验证运算结果,而要同时验证参与运算的每个值。
这个函数的实现很简单,就是跟安全整数的两个边界值比较一下。

```javascript
Number.isSafeInteger = function (n) {
return (typeof n === 'number' &&
Math.round(n) === n &&
Number.MIN_SAFE_INTEGER <= n &&
n <= Number.MAX_SAFE_INTEGER);
}
```

实际使用这个函数时,需要注意。验证运算结果是否落在安全整数的范围内,不要只验证运算结果,而要同时验证参与运算的每个值。

```javascript
Number.isSafeInteger(9007199254740993)
Expand Down
35 changes: 24 additions & 11 deletions docs/proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,29 @@ obj.time // 35
```javascript
var handler = {
get: function(target, name) {
if (name === 'prototype') return Object.prototype;
if (name === 'prototype') {
return Object.prototype;
}
return 'Hello, ' + name;
},
apply: function(target, thisBinding, args) { return args[0]; },
construct: function(target, args) { return args[1]; }

apply: function(target, thisBinding, args) {
return args[0];
},

construct: function(target, args) {
return {value: args[1]};
}
};

var fproxy = new Proxy(function(x, y) {
return x + y;
}, handler);

fproxy(1,2); // 1
new fproxy(1,2); // 2
fproxy.prototype; // Object.prototype
fproxy.foo; // 'Hello, foo'
fproxy(1, 2) // 1
new fproxy(1,2) // {value: 2}
fproxy.prototype === Object.prototype // true
fproxy.foo // "Hello, foo"
```

下面是Proxy支持的拦截操作一览。
Expand Down Expand Up @@ -167,7 +175,7 @@ fproxy.foo; // 'Hello, foo'

拦截Proxy实例作为函数调用的操作,比如`proxy(...args)``proxy.call(object, ...args)``proxy.apply(...)`

**(13)construct(target, args, proxy)**
**(13)construct(target, args)**

拦截Proxy实例作为构造函数调用的操作,比如`new proxy(...args)`

Expand Down Expand Up @@ -500,16 +508,21 @@ for (let b in oproxy2) {

### construct()

`construct`方法用于拦截`new`命令。
`construct`方法用于拦截`new`命令,下面是拦截对象的写法

```javascript
var handler = {
construct (target, args) {
construct (target, args, newTarget) {
return new target(...args);
}
};
```

`construct`方法可以接受两个参数。

- `target`: 目标对象
- `args`:构建函数的参数对象

下面是一个例子。

```javascript
Expand All @@ -525,7 +538,7 @@ new p(1).value
// 10
```

如果`construct`方法返回的不是对象,就会抛出错误
如果`construct`方法返回的必须是一个对象,否则会报错

```javascript
var p = new Proxy(function() {}, {
Expand Down

0 comments on commit 18b2f80

Please sign in to comment.