0

I want to have an object which can act as a registry for calling functions on it.

So far, I have been thinking of something along these lines:

var registry = new Object();

registry.doStuff = function(){ /* */ };

Is there a better way ? What about adding objects ? Ideally, I'd like to be able to add these at run time.

James P.
  • 19,313
  • 27
  • 97
  • 155

3 Answers3

3

How about something along these lines (This is like declaring a class in other languages like Java):

var Registry = function (instanceVariable1) {
    //Initialize any instance variables here (e.g. the registry data)
    this.instanceVariable1 = instanceVariable1;
};

Then declare a function:

Registry.prototype.doStuff = function () {
    //do stuff here
};

Call it like so:

var registry = new Registry(...); //pass in parameters here to initialize the Registry object with data
registry.doStuff();

Brief Explanation: All Javascript objects have a prototype and when you try to access a property via obj['propertyName'], if the property doesn't exist, the prototype will be checked instead. Using a prototype allows you to create new instances of the Registry object without having to re-declare its functions each time. More information: How does JavaScript .prototype work?.

Community
  • 1
  • 1
sushain97
  • 2,752
  • 1
  • 25
  • 36
1

Depending on the situation, you may want to add the functions to the object itself or its prototype.

Adding the functions to the prototype of the object avoids recreating those functions every time a new object is created. Otherwise, the functions are recreated for each new object. Nevertheless, this may be favorable if you want your function to have access to the local variables in the constructor.

Mehmet K
  • 133
  • 3
  • 8
1

To expand on sashain97's answer.

Here is a functional example I created using the prototype method.

        var Registry = function() {
            this.settings = {};
        };
        Registry.prototype.setValue = function(object, path, value) {
            var a = path.split('.');
            var o = object;
            for (var i = 0; i < a.length - 1; i++) {
                var n = a[i];
                if (n in o) {
                    o = o[n];
                } else {
                    o[n] = {};
                    o = o[n];
                }
            }
            o[a[a.length - 1]] = value;
        }

        Registry.prototype.getValue = function(object, path) {
            var o = object;
            path = path.replace(/\[(\w+)\]/g, '.$1');
            path = path.replace(/^\./, '');
            var a = path.split('.');
            while (a.length) {
                var n = a.shift();
                if (n in o) {
                    o = o[n];
                } else {
                    return;
                }
            }
            return o;
        }

        Registry.prototype.set = function(path, value) {
            return this.setValue(this.settings, path, value);
        };

        Registry.prototype.get = function(path) {
            return this.getValue(this.settings, path);
        };

Some examples:

set variable:

    var registry = new Registry();

    registry.set('key1.var1', 'value');
    alert( registry.get('key1.var1') );
    //alerts: value

set method:

    registry.set('key1.f1', function(message, suffix){alert(message + suffix);} );
    registry.get('key1.f1')('hello', ' world');
    //alerts: hello world
Dieter Gribnitz
  • 5,062
  • 2
  • 41
  • 38