0

I have a simple OOP code I started:

(function(window,document){
    var _data = {
        get:function(d){ return _data.data[d] },
        set:function(prop,param){ _data.data[prop]=param },
        remove:function(d){ delete _data.data[d] },
        data:{}
     }; 
     window._data = _data.hasOwnProperty() ? _data.data : _data;
})(window);

What I want done when I type _data alone it'll return _data.data then if I do _data.get(... it'll do what each property needs to do. How is this done?

OR

(function(window,document){
    var _data = {
        get:function(d){ if(!d){return _data.data } else { return _data.data[d] } },
        set:function(prop,param){ _data.data[prop]=param },
        remove:function(d){ delete _data.data[d] },
        data:{}
     }; 
     window._data = _data;
})(window);
EasyBB
  • 6,176
  • 9
  • 47
  • 77

1 Answers1

2

It sounds like you're asking for _data to represent two different things depending upon whether a property was used with it or not. You can't do that in javascript.

You can do this in the global scope:

var _data = {
    get:function(d){ return _data.data[d] },
    set:function(prop,param){ _data.data[prop]=param },
    remove:function(d){ delete _data.data[d] },
    data:{}
 }; 

Then, _data.get() will call that method and return data.

But, when you just refer to _data, you're going to get the whole _data object above, not just _data.data.


The only thing remotely like what you're asking for I can think of would be to make _data be a function like this:

function _data() {
    return _data.data;
}

_data.get = function(d){ return _data.data[d] };
_data.set = function(prop,param){ _data.data[prop]=param };
_data.remove = function(d){ delete _data.data[d] };
_data.data = {};

This works because a function is an object that can have properties.

Then, you could do:

_data()

And that would give you _data.data.

Or, you could do :

_data.get(x)

And that would call the get method.


If you offered some explanation for why you're trying to do this, we could probably offer other ideas.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • So the only way to get all the properties in data would be to do `_data.data` ? – EasyBB Nov 12 '13 at 01:43
  • Excuse my ignorance but, shouldn't the `_data` object be referenced as `this` from within the object itself? – Sebas Nov 12 '13 at 01:43
  • @Sebas I tried that while having it in a different OOP then decided to move it outside of the original OOP though `this` was getting an error – EasyBB Nov 12 '13 at 01:45
  • 2
    @EasyBB - I added another idea to my answer, but what I really wonder is why you're trying to do this. If we knew why or what you're really trying to accomplish, then we could probably come up with more elegant answers. – jfriend00 Nov 12 '13 at 01:46
  • @jfriend00 reasoning for doing this is if we create a variable such as `var a = _data` then we can simple call `a.prop` or we can simply use the _data.get() method. Of course I may be thinking too much about this but hey. Also doing `_data` alone as the object then calling it later though I want this to be easy on my users as they are code illiterate so I want the `_data.get` and `_data.set` for future purposes. – EasyBB Nov 12 '13 at 01:48
  • @Sebas - the OP could use `this` in the methods if they are always called properly, but if this is a static singleton, then it doesn't have to be `this` since there's only ever one of these data items. – jfriend00 Nov 12 '13 at 01:48
  • 2
    @EasyBB if you want to be able to do `a.prop`, then you would do `var a = _data.data;`. It seems like you're overthinking this. Why create get/set accessor methods and then promote not using them by granting direct access to the object? IMO, there's no reason to use accessor methods if direct access to the properties gives you the exact same result. Accessors are generally useful only when they add additional functionality. – jfriend00 Nov 12 '13 at 01:51
  • Well this is going to be a plugin, so my users need to know how to add properties themselves if need be later on. What about `_data.get()` with no `d` argument assigned then that would get all of it? – EasyBB Nov 12 '13 at 01:53
  • They also need to know how to delete a property which yes it is simple though more than likely they won't know how. Updated OP – EasyBB Nov 12 '13 at 01:54
  • Yes, you could code if for `_data.get()` with no argument to return the whole data structure, just check to see if `d === undefined`. FYI, you don't need an accessor to remove a property either. – jfriend00 Nov 12 '13 at 02:51