0

I'm using an object's keys and values to populate other objects like so:

var default_columns = {
    column_a: 'value_a',
    column_b: 'value_b',
    column_c: 'value_c'
// [...]
}

var new_object = {};
for (var key in default_columns) {
    new_object[key] = default_columns[key];
}

But then later on in my program, I would like to resuse those keys as parameters. I could do something like this: new_object['column_a'] but if I change 'column_a' in default_columns I need to update it everywhere in the code.

I thought about defining my object like so:

var default_columns = {
    a: { k: 'column_a', v: 'value_a' },
    b: { k: 'column_b', v: 'value_b' },
    c: { k: 'column_c', v: 'value_c' }
}

and iterate through it as follows:

var new_object = {};
for (var key in default_columns) {
    new_object[default_columns[key].k] = default_columns[key].v;
}

which would also allow me to use the keys as parameters ( new_object[default_columns.a.k] ) while giving me the opportunity to change the keys (e.g. 'column_a' to 'my_column_a') in default_columns without having to update the code.

Is there a more readable way of doing what I'm trying to achieve with the 2nd approach?

2
  • This is a slightly convoluted question. It might help for you to say what you are trying to achieve by mutating objects in this way. What is the goal of your program? Commented Jul 22, 2012 at 20:41
  • What I'm trying to achieve is to use objects (because iterating through them allows me to re-use code) while being able to rename the keys of those objects without having to update the rest of my code where those keys are used as parameters. The reason is that my JavaScript code has external dependencies (those objects are used to defined classes of CSS libraries, columns of a grid plugin...) which change frequently, and I want to be able to update my code accordingly only once and in one place (the object keys) when such changes occur to prevent my code from breaking. Commented Jul 22, 2012 at 21:08

1 Answer 1

1

It seems to me that prototypical inheritance is what you want. Instead of copying all properties from default_columns to new_object with Object.extend, let them inherit from each other (Object.create)!

var new_object = Object.create(default_columns);

// or, in other words:

function Columns(){}
/* var default_columns = */ Columns.prototype = {
    column_a: 'value_a',
    ...
};

var new_object = new Columns();

You then can overwrite some columns on the new_object, which will shadow the inherited properties.


Although when your aim is to easily rename the properties, I'd go with the second approach. Renaming on a normal objects means two lines of code: copy to new and delete old. With a set of objects used everywhere you'd just have to change the "key" property of the objects, and it will reflect to everywhere this particular object is referenced.

Note that your iteration to create a new object won't reflect the changes, as k and v are dereferenced.

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

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.