1

I create objects by two different ways and try to reach them and manipulate them. The object that I create by object constructor it work but same thing I try to do by literal notation doesn't work. I don't understand.

here is code it will explain itself better than me:

var numberOne = new Object();
rand = Math.random();
numberOne[rand] = "random number";
console.log(numberOne); //return 0.6761925128922479: "random number"



var numberTwo = {}; //code doesnt work.
numberTwo.rando = Math.random();
numberTwo[rando] = "another random number";

console.log(numberTwo); //error
codezilla
  • 13
  • 2

5 Answers5

0

It works:

var numberTwo = {};
rand = Math.random();
numberTwo[rand] = "another random number";
console.log(numberTwo);

In your solution numberTwo didn't initialize rand property

Alex Filatov
  • 2,232
  • 3
  • 32
  • 39
0

it doesn't work because your index random is saved into numberTwo object and not in rando variable. You need to refer to index saved into numberTwo object, like this:

 numberTwo[numberTwo.rando];

so, your code must be like this:

var numberTwo = {}; 
numberTwo.rando = Math.random(); // numberTwo.rando contains some random number
numberTwo[numberTwo.rando] = "another random number"; //now it is correct
tfidelis
  • 443
  • 8
  • 16
0

I believe this is what you want:

var numberOne = new Object();
var rand = Math.random();
numberOne[rand] = "random number";
console.log(numberOne);

var numberTwo = {};
rand = Math.random();
numberTwo[rand] = "another random number";

console.log(numberTwo);

Your code breaks at numberTwo[rando] = "another random number"; because rando doesn't exist.

The AOS
  • 81
  • 4
0

Those two are not equivalent.

In the first one, you create a property whose name is a random number and assign that property a string as a value:

rand = Math.random();
numberOne[rand] = "random number";

Your testing shows this: //return 0.6761925128922479: "random number"

In the second one, you create a property called "rando" and assign it a value that is a random number. Then you try to assign a string value to a non-existant property, because you are using rando (a non-existant variable) instead of "rando" (a string) to try to access the property that you created earlier.

numberTwo.rando = Math.random();
numberTwo[rando] = "another random number";

If you used the same approach for both, they would both work. Like this:

var numberOne = new Object();
rand = Math.random();
numberOne[rand] = "random number";
console.log(numberOne);

var numberTwo = {};
rando = Math.random(); // <---- The different line
numberTwo[rando] = "another random number";
console.log(numberTwo);
talemyn
  • 7,822
  • 4
  • 31
  • 52
0

These are not the same things.

In the first case you assign a string to a randomly generated key.

var numberOne = new Object();  // create an object
rand = Math.random();  // make a random number
numberOne[rand] = "random number";  // use the random number as a key name and assign the string as its value

In the second you assign a random number to a key named rando, then try to assign a string to a key that is named as whatever the rando variable is (which is undefined).

var numberTwo = {}; // create an object.
numberTwo.rando = Math.random();  // assign a random number to key named rando
numberTwo[rando] = "another random number";  // here, rando is undefined variable and causes an error.

To make the second example work just do wkat you did in the first one, just change the new Object constructor to an object literal, or:

var numberTwo = {}; // create an object.
numberTwo.rando = Math.random();  // assign a random number to key named rando
numberTwo[numberTwo.rando] = "another random number";   // use the value of numberTwo.rando as a key name

Now numberTwo looks like this:

Object {rando: 0.7977171319071203, 0.7977171319071203: "another random number"}

The key difference here is that object.keyname = x assigns a value to a key named keyname, while object[keyname] = x resolves the value of a variable named keyname and assigns the value to a key named with the value. (for example var keyname = "xy"; object[keyname] = 'x'; will result in an object { xy: 'x' }.

pawel
  • 35,827
  • 7
  • 56
  • 53