@@ -96,21 +96,29 @@ obj.time // 35
9696``` javascript
9797var 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
106114var 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
506514var 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
531544var p = new Proxy (function () {}, {
0 commit comments