0

I feel like this will be one of those "oh jeez, how did I even ask this question" questions, but...

// So I create an object:
var o = {};

// I assign a string value to a variable:
var prop1 = "prop_key";

// And I use that variable that resolves to that string as the property name:
o[prop1] = "value1";

// => So far so good, the property key is the right value:
console.log(o); // => Object {prop_key: "value1"}

// Moving on, I am malicious and overwrite the `prop1` variable, replacing the original value to say, another string:
prop1 = "evil_string";

// And just to make sure it worked:
console.log(prop1); // => evil_value:

// AND YET, when I query the object...
console.log(o); // => Object {prop_key: "value1"}

Shouldn't it now output Object{"evil_value": "val1"} since o[prop1] no longer points to that original prop_key value?

dkugappi
  • 2,664
  • 5
  • 21
  • 22
  • 1
    prop1 refers to a variable not a reference to the object key – Daemedeor Oct 14 '15 at 01:34
  • 1
    When you assign a new value to `prop1`, you lose the original reference. There will be an appropriate duplicate question somewhere round here – Phil Oct 14 '15 at 01:34
  • @Daemedeor Can you expand a bit in terms of what exactly happens? From what you say it sounds like, in step 3, does the value `"prop_key"` of var `prop1` merely get "copied over" as the key? (but it's never an actual reference to the object key) – dkugappi Oct 14 '15 at 12:45

0 Answers0