Although this is not complete, I am experimenting with ES6 Proxies. My goal is to have a constructor function (one that uses classical inheritance, as shown below) to create proxy objects with inheritance goals.
function inherit(child, parent){
Does the following opinion mean? I create an element and return the proxy from the constructor. Any method that returns a reference to an instance (for a chain) should instead return a link to a proxy server, otherwise we will lose a proxy server when chaining.
function Item(attrs){ this.attrs = attrs; var proto = this.constructor.prototype; return this.proxy = Proxy.create(new MyHandler(this), proto); } Item.prototype.setStatus = function(status){ //do work return this.proxy; //we do this everywhere instead of a simple 'this'? } function DVD(attrs){ attrs.type = 'DVD'; return Item.call(this, attrs); } inherit(DVD, Item); var negotiator = new DVD({title: 'The Negotiator'}); //returns proxy
The goal is to create proxied objects (via the "new" keyword), which can be products of the classic inheritance chain.
The next issue. Think about what happens if I extend the prototype of an element using Backbone.Events. Those imported methods return this instead of this.proxy . To get around this, I have to wrap all the imported methods that return this in order to return a proxy . It seems painful and error prone. The use of Proxy abstraction is associated with a large number of wires, which seems to me not quite right.
As long as I love the Proxy concept, I work on its practicality. Maybe I lack the best practice to achieve my goals?
EDIT:
This example was simplified and thus could not describe why I need it.
Ultimately, my goal with proxies is to allow multiple inheritance. I want to be able to mix several types of behavior (modules) into a given proxy object. I also want to just expose the behavior as I see fit.
Using proxies, I can make the proxy object control access to methods and transmit information along with mixins as necessary. In principle, this improves aspect-oriented programming. In aspect, I would usually wrap and replace a function. Sometimes this method is wrapped and processed in several ways. The problem with AOP is that you modified the object in such a way that it is not easy to remove only one aspect, because this aspect may have been buried by ligation.
Proxies do not suffer from this problem. Instead, your mixins can sit in an array. The proxy handles sending to the methods found in these mixes (even several methods, thus multiple inheritance). Then mixing behavior is as simple as removing mixin from this array.
The problem with proxies is that, although it is common practice for a method to return a link to the object itself (for the chain), you really do not want any of the original methods to return a link to this object (thus bypassing the proxy ), you want a proxy server instead.