Proxying the Constructor

Understand how to proxy a constructor and modify it to use in local storage.

With these basics in place, here’s what we want to have a function that will receive a reference to a constructor, e.g., the Contact constructor, and modify it for use with local storage.

Creating a new constructor

So, let’s create a configureStorage function that will do just that:

Press + to interact
Entities.configureStorage = function(constructor){
var OldConstructor = constructor;
var NewConstructor = function(){
var obj = new OldConstructor(arguments[0], arguments[1]);
return obj;
}
NewConstructor.prototype = OldConstructor.prototype;
};

Our configureStorage function takes a constructor as an argument in line 1, assigns it to the OldConstructor variable for readability in line 2, and then defines a new constructor in lines 3–6. This NewConstructor simply creates a new object calling the original constructor and returns the newly created object.

As we know, constructors essentially create a new object, assign it to the this variable used within the constructor definition while assigning its prototype to the constructor’s prototype along the way, and then return the newly created object if no other return value is specified. ...