arrays - name property of object, javascript -
i have question regarding name property of object, here situation: im creating custom object (called notka) prototype.constructor , methods added prototype, im pushing array of objects, converting array json object , saving on external server. far good.
when im getting external server json , converting array of objects name property of object lost , im getting anonymous object dont recognize methods. have few ideas avoid converting arrays objects , viceversa , avoid mess im asking out of curiosity, there way preserve name of object or change name of anonymous object.
i tried creating new object of custom type, like:
var nt = new notka(); // custom object nt = tab[index]; // tab array containing objects fetched external sever
but not work, newly created notka has constructor.name notka when ill assign object have anonymous object again.
thank in advance answer :)
kuba
no, can't serialize json object , preserve name or methods of function (which notka is, not simple object). when saving object database, converted string first. database gets output json.stringify( nt );
. when comes , gets parsed, it's missing except allowed in json standard, not same javascript, node.js or otherwise. it's simpler , allows datatypes (basically plain objects, numbers, strings, , arrays).
it sounds want modify constructor can instead:
var nt = new notka( tab[index] );
if nt = tab[index]
destroying reference original notka object created earlier.
so maybe
var notka = function( obj ) { ( var in obj ) this[i] = obj[i]; }; notka.prototype.multiply = function( times ) { return this.someproperty * times; }; var tab = [{ someproperty: 4, another: "notka!" }, { someproperty: 15, another: "lalalala" }]; var nt = new notka( tab[0] ); nt.multiply( 5 ); // 20
Comments
Post a Comment