Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/krzkaczor/babel-plugin-proxy
Use ES6 proxies today!
https://github.com/krzkaczor/babel-plugin-proxy
babel es6 javascript
Last synced: 5 days ago
JSON representation
Use ES6 proxies today!
- Host: GitHub
- URL: https://github.com/krzkaczor/babel-plugin-proxy
- Owner: krzkaczor
- License: mit
- Created: 2015-12-27T20:31:38.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-11T04:33:10.000Z (over 7 years ago)
- Last Synced: 2024-10-19T16:55:58.144Z (17 days ago)
- Topics: babel, es6, javascript
- Language: JavaScript
- Homepage:
- Size: 10.7 KB
- Stars: 65
- Watchers: 7
- Forks: 6
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# babel-plugin-proxy
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)Use ES6 proxies today!
## Installation
npm install babel-plugin-proxy --save-dev## Motivation
Proxies are awesome feature of ES2015 that enables redefining some language operations. For example we can intercept every object property access with our own function.
The problem is that proper proxy implementation requires native browser support (currently it works in Firefox and Edge). This plugin is proof of concept that proxies can be implemented with ES5 features. It is not suitable for production environments because performance impact is huge.
## How does it work?We are intercepting every property access (except these connected with function invocation) and property assignment with custom interceptor functions that performs runtime check if object is proxied.
proxy.foo = 5;
proxy.foo;
becomes:
globalSetInterceptor(proxy, "foo", 5);
globalGetInterceptor(proxy, 'foo');
These interceptors performs runtime check if object should be proxied. You can check out whole runtime [here](https://github.com/krzkaczor/babel-plugin-proxy/blob/master/src/runtime.js)## Example
Proxies for example allow us to create objects that will warn us when `undefined` key is being accessed.
var proxy = new Proxy({}, {
get: function(target, propKey) {
if (!(propKey in target)) {
console.log("Accessing undefined key!");
}
return target[propKey];
}
});
proxy.a = 5;
console.log(proxy.a);
console.log(proxy.b);output:
5
Accessing undefined key!
undefined