1

I would just like to know how to access the value of another key in the same hash. from inside the hash. not by doing myHash.key2 = myHash.key1....; I mean a way of doing :

var myHash = {
    key1: 5,
    key2: key1 * 7,
    key3: true,
    key4: (key3)? "yes" : "no"
};

PS: this is only a simplified version of the actual code in fact every key has some complex operations inside. The values and not simple Numbers or bools.

Anthony N.
  • 295
  • 3
  • 16
  • 2
    use functions. ie for key2 would be `function() { return myHash.key1 * 7; }` – r3wt Nov 05 '15 at 01:28
  • @r3wt That's a good idea but that would make me use () to access the values of these keys and not use them to access the values of the other ones. So if i wana iterate over the hash it wouldnt be easy. Unless i turn everything to functions. Any ideas on that ? – Anthony N. Nov 05 '15 at 01:33

2 Answers2

4

You cannot refer to other keys on the object within a literal definition. The options for setting a key based on other keys or other values within the object are:

  1. Use a getter function for the key that can return a value based on other properties.

  2. Use a regular function for the key that can return a value based on other properties.

  3. Assign a key/value outside of the literal definition where you can assign a static value based on the other keys/properties.

Here are examples of each of these options:

// use getters for properties that can base their values on other properties
var myHash = {
    key1: 5,
    get key2() { return this.key1 * 7; },
    key3: true,
    get key4() { return this.key3 ? "yes" : "no";}
};

console.log(myHash.key2);    // 35

// use regular functions for properties that can base 
// their values on other properties
var myHash = {
    key1: 5,
    key2: function() { return this.key1 * 7; },
    key3: true,
    key4: function() { return this.key3 ? "yes" : "no";}
};

console.log(myHash.key2());    // 35

// assign static properties outside the literal definition
// that can base their values on other properties
var myHash = {
    key1: 5,
    key3: true
};
myHash.key2 = myHash.key1 * 7;
myHash.key4 = myHash.key3 ? "yes" : "no";

console.log(myHash.key2);    // 35

Note: the first two options are "live". If you change the value of myHash.key1, then the value of myHash.key2 or myHash.key2() will change too. The third option is static and the values of myHash.key2 will not follow changes in myHash.key1.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    @OlaviSau - you can't access another key in the middle of a literal definition? The variable `myHash` is not set yet and `this` is not set to the object either. So, you have no way to refer to other keys of the object from within a literal definition. If you're going to do it in the literal definition, you have to use properties that are a function (either regular functions or getters). – jfriend00 Nov 05 '15 at 01:38
  • Yeaaaah...just something odd happened, it worked in console the first time I tried, and then i tested one of your examples and then it didn't i must have missed something, I know what mistake I made now, I tried using this first, that created an obj and then the second time it used that object's var, you are indeed correct. – Olavi Sau Nov 05 '15 at 01:40
  • May want to use `Object.defineProperties` instead. – StackSlave Nov 05 '15 at 01:45
  • I think the getters would be the best option for me. because a function of the style : for(var key in myHash)console.log(myHash[key]); would work no matter if it's a function or not. any idea in terms of performance (speed)? – Anthony N. Nov 05 '15 at 01:54
  • @AnthonyN. - I have not done a performance comparison on getters vs. static properties. You could do one yourself fairly quickly in http://jsperf.com in your favorite browser. Just remember that getters require IE9 or greater. – jfriend00 Nov 05 '15 at 02:08
0

First of all you need to refer to Object properties with the keyword this. Do this instead:

var myHash = {
  key1: 5,
  key2: 11,
  key3: true,
  isKey3true: function(){
    var r = this.key3 ? 'yes' : 'no';
    return r;
  }
}
myHash.key1 = 100;
console.log(key2); // key2 not a method
myHash.key3 = false;
console.log(myHash.isKey3true()); // method will review key3 value
StackSlave
  • 10,613
  • 2
  • 18
  • 35