Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proxy substitue for ES5

Is it possible to listen to property changes without the use of Proxy and setInterval?

For common objects you could use the function below but that works for all existing properties but doesn't work for any properties that might get added after the wrapping.

function wrap(obj) {
  var target = {};
  Object.keys(obj).forEach(function(key) {
    target[key] = obj[key];
    Object.defineProperty(obj, key, {
      get: function() {
        console.log("Get");
        return target[key];
      },
      set: function(newValue) {
        console.log("Set");
        target[key] = newValue;
      }
    });
  });
}

var obj = {
  a: 2,
  b: 3
};
wrap(obj);

obj.a; // Get
obj.a = 2; // Set
obj.b; // Get
obj.b = 2; // Set
obj.c = 2; // Nothing
obj.c; // Nothing

If the object is an array you could also listen to the length property and reset all the get and set functions when it's changed. This is obviously not very efficient as it changes the properties of each element whenever an element is added or removed.

So I don't think that Object.defineProperty is the answer.

The reason I don't want to use setInterval is because having big intervals will make the wrapping unreliable whereas having small intervals will have a big impact on the efficiency.

like image 489
nick zoum Avatar asked Mar 26 '19 14:03

nick zoum


People also ask

What are ES6 proxies?

ES6 proxies sit between your code and an object. A proxy allows you to perform meta-programming operations such as intercepting a call to inspect or change an object's property. The following terminology is used in relation to ES6 proxies: target. The original object the proxy will virtualize.

What is proxy in console log?

Description. The Proxy object allows you to create an object that can be used in place of the original object, but which may redefine fundamental Object operations like getting, setting, and defining properties. Proxy objects are commonly used to log property accesses, validate, format, or sanitize inputs, and so on.

What are proxy objects?

A proxy object acts as an intermediary between the client and an accessible object. The purpose of the proxy object is to monitor the life span of the accessible object and to forward calls to the accessible object only if it is not destroyed.

What is proxy target?

A JavaScript Proxy is an object that wraps another object (target) and intercepts the fundamental operations of the target object. The fundamental operations can be the property lookup, assignment, enumeration, and function invocations, etc.

What is the difference between substitute and proxy?

is that substitute is to use in place of something else, with the same function while proxy is to serve as a proxy for.

Does ESXi support passwords for web proxies?

ESXi does not support Web proxies that use passwords or pass phrases, also known as encrypted keys. If you set up a Web proxy that requires a password or pass phrase, ESXi processes cannot start correctly. To support encryption for user names, passwords, and packets, SSL is enabled by default for vSphere Web Services SDK connections.

What should I consider when modifying my Web Proxy settings?

When you modify Web proxy settings, you have several encryption and user security guidelines to consider. Note: Restart the host process after making any changes to host directories or authentication mechanisms. Do not set up certificates that use a password or pass phrases.

Should I disable SSL on my ESXi Server?

Disabling SSL can improve performance, because you avoid the overhead required to perform encryption. To protect against misuse of ESXi services, most internal ESXi services are accessible only through port 443, the port used for HTTPS transmission. Port 443 acts as a reverse proxy for ESXi.


1 Answers

Sadly no, that's why Proxies were such a big thing. There is no other way, for now, to trigger code when a property is added to an object than Proxy.

As you say, you can use Object.defineProperty or var a = { get x() {...}, set x(value) {...} } but not detect new properties.


Most frameworks rely on dirty-check: comparing objects on a giving timing. The timing is where the difference mainly is.

AngularJS (Angular 1.x) gave you special functions for asynchronous operations like $timeout and $http and it's own way to listen to DOM events that will wrap your callbacks and run the check after your code.

Angular (Angular 2 to N) uses Zone.js to create a "Running context" for your code, any asynchronous callback is intercepted by Zone.js. It's basically the same solution as for AngularJS but works automagically.

React does something similar but instead of tracking your variables it runs the renderer and compares if the generated DOM (Virtual DOM) is different.

like image 199
A. Matías Quezada Avatar answered Oct 03 '22 13:10

A. Matías Quezada