JavaScript - Adding an object into an object -
i realise has been asked before cannot find exact answer question.
i'm trying recreate object in javascript. original object looks this:
stock { id: 'a0236000374782', product { name: 'gum', price: 2.49 } }
when attempt recreate stock object have first value id in , trying somehow push product object in well. here's have attempted:
var product = {}; stock.product.name = 'gum';
this returns cannot set property name of undefined. tried:
var product = {}; stock = {product.name : 'gum'};
this returns "uncaught syntaxerror: unexpected token ".". cannot figure out not doing right.
you can create objects in javascript.
assuming have stock defined looking this:
var stock = { id: '1234' };
you can assign product follows:
stock.product = { name: 'gum', price: 2.49 };
that's same doing this:
var product = { name: 'gum', price: 2.49 }; stock.product = product;
but can't assign property onto object hasn't been created. isn't possible:
stock.uncreatedobject.myprop = 'will not work';
but possible:
stock.createdobject = {}; stock.createdobject.myprop = 'this work';
which equivalent this:
stock.createdobject = { myprop: 'will work' };
Comments
Post a Comment