es6-proxy

ES6 Proxy Polyfill for IE11

这一生的挚爱 提交于 2019-12-30 01:36:38
问题 IE11 does not and will not implement ES2015 Proxy objects. Yet IE11's end of extended support is October 14, 2025. Is there any way to polyfill Proxy objects for IE11? All other browsers support Proxy already. If yes then we would all be able to use it in production today. If not then we'll have to wait almost a decade... Edit: I'm asking specifically for IE11 as I know IE to usually have IE specific features that I'm often not aware of. Edit2: I'm particularly interested in being able to

How to get proxy's handler from proxy object?

拥有回忆 提交于 2019-12-18 04:39:05
问题 For example, if I have this handler/proxy (from the MDN example)... var handler = { get: function(target, name){ return name in target? target[name] : 37; } }; var p = new Proxy({}, handler); p.a = 1; p.b = undefined; console.log(p.a, p.b); // 1, undefined console.log('c' in p, p.c); // false, 37 is it possible to probe the proxy, p , in some way that allows me to get the handler object back. Something along the lines of: p.__handler__ // returns handler object -> Object {get: handler.get(),

Use ES6 proxy to trap Object.hasOwnProperty

*爱你&永不变心* 提交于 2019-12-13 15:46:10
问题 I want to use an ES6 proxy to trap the following common code: for (let key in trapped) { if (!Object.prototype.hasOwnProperty.call(obj, key)) continue; let value = trapped[key]; //various code } But after reviewing the proxy documentation, I'm not sure how to do it, mainly because the has trap trap is for the in operator, which does not seem to be used in the above code and there is no trap for the hasOwnProperty operation. 回答1: You can use the getOwnPropertyDescriptor handler to capture

sessionStorage proxy class scope

喜夏-厌秋 提交于 2019-12-13 01:07:06
问题 I am wondering how to access the native sessionStorage scope from within my custom methods. My example: https://jsfiddle.net/3mc7ao7j/1/ At line 3 I would like to be able to proxy through to my native sessionStorage after perform my mutation on the data. How do I access that scope again though? I know I can just call: sessionStorage.setItem() But that simply works because it is globally available and it feels wrong to do that. Mainly because I would also want to know how to do this without an

Check if proxy object is revoked

﹥>﹥吖頭↗ 提交于 2019-12-12 03:39:45
问题 ECMAScript 6 introduces proxy object, which may be created as revocable. How can I detect if a proxy has been revoked? 回答1: The Proxy constructor only accepts targets and handlers when they are objects and are not revoked proxies. From ProxyCreate, If Type( target ) is not Object, throw a TypeError exception. If target is a Proxy exotic object and the value of the [[ProxyHandler]] internal slot of target is null, throw a TypeError exception. This allows you to check if a value is a revoked

Proxying a recursive function

徘徊边缘 提交于 2019-12-10 17:12:08
问题 Imagine a simple recursive function, which we are trying to wrap in order to instrument input and output. // A simple recursive function. const count = n => n && 1 + count(n-1); // Wrap a function in a proxy to instrument input and output. function instrument(fn) { return new Proxy(fn, { apply(target, thisArg, argumentsList) { console.log("inputs", ...argumentsList); const result = target(...argumentsList); console.log("output", result); return result; } }); } // Call the instrumented

TypeError when trying to Proxy a ES6 class constructor

强颜欢笑 提交于 2019-12-10 12:15:02
问题 I'm trying to Proxy a ES6 constructor (mostly trying to emulate Python's descriptors for fun and learning): class _Record { constructor(data) { this._data = data; } } const constrProxyHandlers = { construct: function(target, args, cstr) { let instProxyHandler = { get: (target, prop) => { if(prop in target._data) { return target._data[prop] } return target[prop] }, set: (target, prop, val) => { if(prop in target._data) { target._data[prop] = val; } else { target[prop] = val; } return true } }

Custom Array-like getter in JavaScript

ⅰ亾dé卋堺 提交于 2019-12-09 10:20:12
问题 I have a simple ES6 class, like so: class Ring extends Array { insert (item, index) { this.splice(index, 0, item); return this; } } I want to make it so that the indexing for Ring objects wraps, so that new Ring(1, 2, 3)[3] returns 1, new Ring(1, 2, 3)[-1] returns 3, and so on. Is this possible in ES6? If so, how would I implement it? I've read about proxies, which allow a completely customized getter, but I can't figure out how to apply a proxy to a class. I did manage this: var myRing = new

Illegal invocation error using ES6 Proxy and node.js

£可爱£侵袭症+ 提交于 2019-12-09 03:23:19
问题 I can not figure out why the following code does not work: var os = new Proxy(require('os'), {}); console.log( os.cpus() ); // TypeError: Illegal invocation whereas var os = require('os'); console.log(Reflect.apply(os.cpus, os, [])); or var os = new Proxy(require('os'), {}); console.log( os.platform() ); works as expected. 回答1: Having just skim read the source for the os package in the Node repo, it appears that the cpus() is exported from binding.getCPUs which is a C hook in the Node runtime

Returning ES6 Proxy from the ES6 class constructor

痞子三分冷 提交于 2019-12-04 16:25:52
问题 I want user to only set specific properties to an object but as the same time that object should be constructed from custom class. For example var row = new Row({ name : 'John Doe', email : 'uhiwarale@gmail.com' }, Schema); row can have methods. But when user is trying to set row.password , they are not allowed. One way to do it is using new Proxy instead of new Row but then we will loose all cool things we are doing inside Row class. I want new Row to return a proxy object with this