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/

4
  • stackoverflow.com/questions/20147081/… Commented Sep 29, 2018 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. Commented Sep 29, 2018 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. Commented Sep 29, 2018 at 21:03
  • @Dave Newton, I tend to agree. Thanks for the validation. Commented Sep 29, 2018 at 21:03

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.