JavaScript object basics

learning

(Modestas) #1

Hi in this article it states:
“Adding a property to an object using the method above isn’t possible with dot notation, which can only accept a literal member name, not a variable value pointing to a name.”

Im using an online text editor called “repl” and i tried adding properties to object variables using dot notation, it worked! Is this a new ES6 feature of javaScript?


(jsx) #2

This is what the page says-

var person = {};
var myDataName = 'height';
var myDataValue = '1.75m';

This won’t work, because it uses the dot notation:
(See that person.height is undefined)

person.myDataName = myDataValue;
console.log(person.height); // will be undefined
console.log(person.myDataName); // will be '1.75m'
console.log(person['myDataName']); // will be '1.75m'

But this will work, using bracket notation:

person[myDataName] = myDataValue;
console.log(person.height);  // will be '1.75m'