es6-proxy

Custom Array-like getter in JavaScript

守給你的承諾、 提交于 2019-12-03 12:09:40
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 Proxy (Ring.prototype, { get: function (target, name) { var len = target.length; if (/^-?\d+$/.test

Illegal invocation during non configurable method call with javascript proxies

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 19:16:54
问题 I am using javascript proxies to intercept method calls to an object, however if the method is a non configurable and non writable property, I am not able to correctly intercept it var handler = { get(target, key, receiver) { if (target[key] && (typeof target[key] === 'object' || typeof target[key] === "function")) { var desc = Object.getOwnPropertyDescriptor(target, key); if (desc && ! desc.configurable && !desc.writable) return Reflect.get(target,key); var method = Reflect.get(target, key);

Illegal invocation error using ES6 Proxy and node.js

丶灬走出姿态 提交于 2019-12-01 09:11:36
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. 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 environment. cpus() therefore has the binding object as a function context, which is then lost through the

Can I extend Proxy with an ES2015 class?

一笑奈何 提交于 2019-11-27 12:00:02
问题 I tried to extend Proxy, like so: class ObservableObject extends Proxy {} I used Babel to transpile it to ES5, and I got this error in the browser: app.js:15 Uncaught TypeError: Object prototype may only be an Object or null: undefined I looked at the line of code it pointed to. Here's that portion of the code with arrows pointing to the offending line of code: var ObservableObject = exports.ObservableObject = function (_Proxy) { _inherits(ObservableObject, _Proxy); // <<<<<<<<<<<<<<<<<<<<<<<

How to use javascript proxy for nested objects

*爱你&永不变心* 提交于 2019-11-27 11:30:50
问题 I have this code in js bin: var validator = { set (target, key, value) { console.log(target); console.log(key); console.log(value); if(isObject(target[key])){ } return true } } var person = { firstName: "alfred", lastName: "john", inner: { salary: 8250, Proffesion: ".NET Developer" } } var proxy = new Proxy(person, validator) proxy.inner.salary = 'foo' if i do proxy.inner.salary = 555; it does not work. However if i do proxy.firstName = "Anne" , then it works great. I do not understand why it