Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning ES6 Proxy from the ES6 class constructor

Tags:

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 : '[email protected]' }, 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 reference as a target of proxy.

Anybody have any ideas on this? If you know mongoose, how mongoose is doing it?

like image 928
Uday Hiwarale Avatar asked Dec 04 '16 18:12

Uday Hiwarale


People also ask

What is ES6 proxy?

ES6 implements intercession form of meta programming using Proxies. Similar to ReflectAPI, the Proxy API is another way of implementing meta programming in ES6. The Proxy object is used to define custom behavior for fundamental operations. A proxy object performs some operations on behalf of the real object.

What is constructor in ES6?

A constructor is a function that is called each time an object is created (also referred to as instantiated). The User constructor creates the properties of the object (this.name, this. age, this. email) and assigns them the value of the parameters passed to it (name, age, email).

What does a proxy do to the target object Ecmascript?

A proxy allows you to perform meta-programming operations such as intercepting a call to inspect or change an object's property. The original object the proxy will virtualize.

What is the difference between proxy constructor and reflect?

So Proxy is a wrapper which can be used to intercept fundamental operations like [[Get]] and [[Set]] on an object whereas Reflect provides us minimal wrappers around these fundamental operations like [[Get]] and [[Set]] so that we can call them directly (Usually from inside the trap).


2 Answers

If the proxy is certain to happen for you, one possible solution to limit the set functionality is returning an ES6 Proxy instance.

By default, the constructor in javascript returns this object automatically but you could define and return a custom behavior by instantiating a proxy on this as a target. Keep in mind that the set method in proxy should return a boolean value.

MDN: The set method should return a boolean value. Return true to indicate that assignment succeeded. If the set method returns false, and the assignment happened in strict-mode code, a TypeError will be thrown.

class Row {   constructor(entry) {     // some stuff      return new Proxy(this, {       set(target, name, value) {         let setables = ['name', 'email'];         if (!setables.includes(name)) {           throw new Error(`Cannot set the ${name} property`);         } else {           target[name] = value;           return true;         }       }     });   }    get name() {     return this._name;   }   set name(name) {     this._name = name.trim();   }   get email() {     return this._email;   }   set email(email) {     this._email = email.trim();   } } 

So, now you are not allowed to set the non-setable properties according to the proxy.

let row = new Row({   name : 'John Doe',   email : '[email protected]' });  row.password = 'blahblahblah'; // Error: Cannot set the password property 

It's also possible to have s custom behavior on get method too.

However, beware and take care of overriding the reference that is returned to the calling context.

Note: The sample code has been tested on Node v8.1.3 and modern browsers.

like image 110
Vahid Hallaji Avatar answered Oct 15 '22 13:10

Vahid Hallaji


You can do this without using Proxies at all.

In your class constructor you can define the password property like this:

constructor(options, schema) {     this.name = options.name;     this.email = options.email;     Object.defineProperty(this, 'password', {         configurable: false, // no re-configuring this.password         enumerable: true, // this.password should show up in Object.keys(this)         value: options.password, // set the value to options.password         writable: false // no changing the value with this.password = ...     });     // whatever else you want to do with the Schema } 

You can find more information about how to use this on the MDN's Object.defineProperty() page.

like image 39
Joshua Skrzypek Avatar answered Oct 15 '22 12:10

Joshua Skrzypek