0

I created an object that requires a dictionary object be passed to initialize, and has attributes (like length). I'd like to be able to simply access the dictionary's key : value pair by passing something like:

myNewObj['a'] or myNewObj.a

and not having to instead pass myNewObj.dictionary['a'], but can't figure out how to do so.

Is there a way to set a dynamic attribute that, if no other attribute or method exits, will instead look into the dictionary and find the associated key?

var newObject = function(dictionary) {
    this.dictionary     = dictionary;
    this.length         = dictionary[[Object.keys(dictionary)[0]]].length;

    this.get = function (column) {
        return this.dictionary[[column]];
    }
};

var myNewObj = new newObject({
    a   : [1,2,3],
    b   : [4,5,6]
});

console.log(myNewObj.get('a'));

I've updated this to show a .get() method that works, but it's still not exactly what I'm looking for.

jsFiddle: http://jsfiddle.net/2uMjv/571/

thebjorn
  • 26,297
  • 11
  • 96
  • 138
elPastor
  • 8,435
  • 11
  • 53
  • 81
  • https://stackoverflow.com/questions/20147081/javascript-catch-access-to-property-of-object – thebjorn Sep 29 '18 at 20:39
  • @thebjorn - if that's the only way to do what I'm looking for, I may just stick with the `.get()` method, but I appreciate the reference. – elPastor Sep 29 '18 at 20:47
  • Since you cannot override operators (e.g., `[]`) there's no clean way to do this. Proxies *might* work, but you run the risk of removing access to actual properties, particularly if you don't know what will be in the "dictionary". There are ways you could avoid that, but IMO, more trouble than it's worth. – Dave Newton Sep 29 '18 at 21:03
  • @Dave Newton, I tend to agree. Thanks for the validation. – elPastor Sep 29 '18 at 21:03

2 Answers2

0

Although this might not exactly fit your use case of dynamic attributes, the closest thing I can think of is extending the prototype. At least by using Object.setPrototypeOf* you can avoid defining each method individually and have a base set of functions to apply to each one of these newObject's.

var newObject = function(dictionary) {
    this.dictionary = dictionary;
    this.length = dictionary[[Object.keys(dictionary)[0]]].length;

    this.get = function(column) {
        return this.dictionary[[column]];
    }
};

// Essentially a wrapper constructor.
var createNewObject = function(dictionary) {
    Object.setPrototypeOf(dictionary, new newObject(dictionary));
    return dictionary;
}

var myNewObj = createNewObject({
    a   : [1,2,3],
    b   : [4,5,6]
});

console.log(myNewObj['a']);
console.log(myNewObj.get('b'));

*You may want to look at the potential drawbacks in using this.

woat
  • 431
  • 3
  • 8
  • thanks for the code. I'm pretty new to objects in javascript, so I'll need some time to wrap my head around this before I can tell whether it does what I need. – elPastor Sep 29 '18 at 21:33
0

Why don't you just set properties? :

 class Dictionary {
  constructor(dictionary) {
   Object.assign(this, dictionary);
  }

  get length() {
    return Object.keys(this).length;
  }

  get(column) {
    return this[column];
  }
}

var dict = new Dictionary({
  a   : [1,2,3],
  b   : [4,5,6]
});

console.log(dict.a, dict["a"], dict.get("a"));
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151