Skip to content

Commit 18b2f80

Browse files
committed
docs(proxy): construct方法
1 parent f7494bc commit 18b2f80

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

docs/number.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ ES5可以通过下面的代码,部署`Number.isFinite`方法。
6565
})(this);
6666
```
6767

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

7070
```javascript
7171
Number.isNaN(NaN) // true
@@ -236,6 +236,8 @@ Number.MIN_SAFE_INTEGER === -9007199254740991
236236
// true
237237
```
238238

239+
上面代码中,可以看到JavaScript能够精确表示的极限。
240+
239241
`Number.isSafeInteger()`则是用来判断一个整数是否落在这个范围之内。
240242

241243
```javascript
@@ -256,7 +258,18 @@ Number.isSafeInteger(Number.MAX_SAFE_INTEGER) // true
256258
Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1) // false
257259
```
258260

259-
注意,验证运算结果是否落在安全整数的范围时,不要只验证运算结果,而要同时验证参与运算的每个值。
261+
这个函数的实现很简单,就是跟安全整数的两个边界值比较一下。
262+
263+
```javascript
264+
Number.isSafeInteger = function (n) {
265+
return (typeof n === 'number' &&
266+
Math.round(n) === n &&
267+
Number.MIN_SAFE_INTEGER <= n &&
268+
n <= Number.MAX_SAFE_INTEGER);
269+
}
270+
```
271+
272+
实际使用这个函数时,需要注意。验证运算结果是否落在安全整数的范围内,不要只验证运算结果,而要同时验证参与运算的每个值。
260273

261274
```javascript
262275
Number.isSafeInteger(9007199254740993)

docs/proxy.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,29 @@ obj.time // 35
9696
```javascript
9797
var handler = {
9898
get: function(target, name) {
99-
if (name === 'prototype') return Object.prototype;
99+
if (name === 'prototype') {
100+
return Object.prototype;
101+
}
100102
return 'Hello, ' + name;
101103
},
102-
apply: function(target, thisBinding, args) { return args[0]; },
103-
construct: function(target, args) { return args[1]; }
104+
105+
apply: function(target, thisBinding, args) {
106+
return args[0];
107+
},
108+
109+
construct: function(target, args) {
110+
return {value: args[1]};
111+
}
104112
};
105113

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

110-
fproxy(1,2); // 1
111-
new fproxy(1,2); // 2
112-
fproxy.prototype; // Object.prototype
113-
fproxy.foo; // 'Hello, foo'
118+
fproxy(1, 2) // 1
119+
new fproxy(1,2) // {value: 2}
120+
fproxy.prototype === Object.prototype // true
121+
fproxy.foo // "Hello, foo"
114122
```
115123

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

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

170-
**(13)construct(target, args, proxy)**
178+
**(13)construct(target, args)**
171179

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

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

501509
### construct()
502510

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

505513
```javascript
506514
var handler = {
507-
construct (target, args) {
515+
construct (target, args, newTarget) {
508516
return new target(...args);
509517
}
510518
};
511519
```
512520

521+
`construct`方法可以接受两个参数。
522+
523+
- `target`: 目标对象
524+
- `args`:构建函数的参数对象
525+
513526
下面是一个例子。
514527

515528
```javascript
@@ -525,7 +538,7 @@ new p(1).value
525538
// 10
526539
```
527540

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

530543
```javascript
531544
var p = new Proxy(function() {}, {

0 commit comments

Comments
 (0)