|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2018 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# Proxy |
| 22 | + |
| 23 | +> [Proxy][mdn-proxy] object. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var Proxy = require( '@stdlib/proxy/ctor' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### Proxy( target, handlers ) |
| 44 | + |
| 45 | +Returns a [proxy][mdn-proxy] object implementing custom behavior for specified object operations. |
| 46 | + |
| 47 | +```javascript |
| 48 | +function get( target, property ) { |
| 49 | + return target[ property ] * 2.0; |
| 50 | +} |
| 51 | + |
| 52 | +var handlers = { |
| 53 | + 'get': get |
| 54 | +}; |
| 55 | + |
| 56 | +var p = new Proxy( {}, handlers ); |
| 57 | + |
| 58 | +p.a = 3.14; |
| 59 | + |
| 60 | +var v = p.a; |
| 61 | +// returns 6.28 |
| 62 | +``` |
| 63 | + |
| 64 | +A `handlers` argument should be an `object` whose properties are functions (called "**traps**") which define the behavior of the proxy when performing operations. The following traps are supported: |
| 65 | + |
| 66 | +- `getPrototypeOf( target )` |
| 67 | + |
| 68 | + - Trap for `Object.getPrototypeOf()`. Can be used to intercept the `instanceof` operator. The method must return an `object` or `null`. |
| 69 | + |
| 70 | +- `setPrototypeOf( target, prototype )` |
| 71 | + |
| 72 | + - Trap for `Object.setPrototypeOf()`. The method must return a `boolean` indicating if prototype successfully set. |
| 73 | + |
| 74 | +- `isExtensible( target )` |
| 75 | + |
| 76 | + - Trap for `Object.isExtensible()`. The method must return a `boolean`. |
| 77 | + |
| 78 | +- `preventExtensions( target )` |
| 79 | + |
| 80 | + - Trap for `Object.preventExtensions()`. The method must return a `boolean`. |
| 81 | + |
| 82 | +- `getOwnPropertyDescriptor( target, property )`` |
| 83 | + |
| 84 | + - Trap for `Object.getOwnPropertyDescriptor()`. The method must return an `object` or `undefined`. |
| 85 | + |
| 86 | +- `defineProperty( target, property, descriptor )` |
| 87 | + |
| 88 | + - Trap for `Object.defineProperty()`. The method must return a `boolean` indicating whether the operation succeeded. |
| 89 | + |
| 90 | +- `has( target, property )` |
| 91 | + |
| 92 | + - Trap for the `in` operator. The method must return a `boolean`. |
| 93 | + |
| 94 | +- `get( target, property, receiver )` |
| 95 | + |
| 96 | + - Trap for retrieving property values. The method can return any value. |
| 97 | + |
| 98 | +- `set( target, property, value, receiver )` |
| 99 | + |
| 100 | + - Trap for setting property values. The method should return a `boolean` indicating whether assignment succeeded. |
| 101 | + |
| 102 | +- `deleteProperty( target, property )` |
| 103 | + |
| 104 | + - Trap for the `delete` operator. The method must return a `boolean` indicating whether operation succeeded. |
| 105 | + |
| 106 | +- `ownKeys( target )` |
| 107 | + |
| 108 | + - Trap for `Object.keys`, `Object.getOwnPropertyNames()`, and `Object.getOwnPropertySymbols()`. The method must return an enumerable `object`. |
| 109 | + |
| 110 | +- `apply( target, thisArg, argumentsList )` |
| 111 | + |
| 112 | + - Trap for a function call. The method can return any value. |
| 113 | + |
| 114 | +- `construct( target, argumentsList, newTarget )` |
| 115 | + |
| 116 | + - Trap for the `new` operator. The method must return an `object`. |
| 117 | + |
| 118 | +All traps are **optional**. If a trap is not defined, the default behavior is to forward the operation to the target. |
| 119 | + |
| 120 | +</section> |
| 121 | + |
| 122 | +<!-- /.usage --> |
| 123 | + |
| 124 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 125 | + |
| 126 | +<section class="notes"> |
| 127 | + |
| 128 | +</section> |
| 129 | + |
| 130 | +<!-- /.notes --> |
| 131 | + |
| 132 | +<!-- Package usage examples. --> |
| 133 | + |
| 134 | +<section class="examples"> |
| 135 | + |
| 136 | +## Examples |
| 137 | + |
| 138 | +<!-- eslint no-undef: "error" --> |
| 139 | + |
| 140 | +```javascript |
| 141 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 142 | +var Proxy = require( '@stdlib/proxy/ctor' ); |
| 143 | + |
| 144 | +var handlers; |
| 145 | +var arr; |
| 146 | +var p; |
| 147 | +var i; |
| 148 | + |
| 149 | +// Create a new typed array: |
| 150 | +arr = new Float64Array( 10 ); |
| 151 | +for ( i = 0; i < arr.length; i++ ) { |
| 152 | + arr[ i ] = i; |
| 153 | +} |
| 154 | + |
| 155 | +// Define a "trap" when retrieving property values: |
| 156 | +function get( obj, prop ) { |
| 157 | + if ( prop === 'length' ) { |
| 158 | + return obj.length; |
| 159 | + } |
| 160 | + return obj[ prop ] * 2.0; |
| 161 | +} |
| 162 | + |
| 163 | +// Define the proxy handlers: |
| 164 | +handlers = { |
| 165 | + 'get': get |
| 166 | +}; |
| 167 | + |
| 168 | +// Create a proxy: |
| 169 | +p = new Proxy( arr, handlers ); |
| 170 | + |
| 171 | +// Access array values... |
| 172 | +for ( i = 0; i < p.length; i++ ) { |
| 173 | + console.log( 'arr[%d] = %d', i, p[ i ] ); |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +</section> |
| 178 | + |
| 179 | +<!-- /.examples --> |
| 180 | + |
| 181 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 182 | + |
| 183 | +<section class="references"> |
| 184 | + |
| 185 | +</section> |
| 186 | + |
| 187 | +<!-- /.references --> |
| 188 | + |
| 189 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 190 | + |
| 191 | +<section class="links"> |
| 192 | + |
| 193 | +[mdn-proxy]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy |
| 194 | + |
| 195 | +</section> |
| 196 | + |
| 197 | +<!-- /.links --> |
0 commit comments