javascript dynamic property accessor 1

javascript dynamic property accessor

var something = {
  bar: 'foo'
};
var foo = 'bar';

// both x = something[foo] and something[foo] = x work as expected
console.log(something[foo]);
console.log(something.bar)

Here is what the above code is Doing:
1. We create an object called something.
2. We create a variable called foo.
3. We access the property bar of the object something using the variable foo.
4. We access the property bar of the object something using the dot notation.

Similar Posts