javascript - JS object toString() returns constructor.name and data which I cannot retrieve in any other way -
my question adobe js specific sure relevance more general.
to begin, please consider particular case: adobe js api 3d has object called matrix4x4. in api reference, can read 5 properties: determinant, inverse, scalecomponent, translation , transpose.
in code, have 1 matrix4x4 object. running "for (var in matrix4x4)", aforementioned properties. matrix4x4[i], might inverse matrix example.
however, matrix4x4.tostring() gives original matrix values need in following string format:
matrix4x4: (1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)
matrix4x4.tosource() strangely gives ({}) i.e. empty object.
now, aim write function or prototype values of matrix .tostring() without heading "matrix4x4:" (note matrix4x4 equal constructor.name) , no line feeds (return carriages). tried use eval(matrix4x4.tostring()), eval("{"+matrix4x4.tostring()+"}") or eval("({"+matrix4x4.tostring()+"})") seems string not formed. suppose there should 2 ' enclose matrix values , no line feeds.
is possible either override .tostring() method or write custom prototype retrieve same values printed present .tostring() in such strange format close literal object?
as said, have not succeeded in reaching object values given .tostring() in other way must somewhere inside object because .tostring() shows them , think arguments of other properties. example, .determinant calculated on them.
thanks
not sure you're trying do, want string trying use eval create object. maybe trick:
matrix4x4.prototype.toobject=function(){ return{ determinant:this.determinant, inverse:this.inverse, scalecomponent:this.scalecomponent, translation:this.translation. transpose:this.transpose }; }
if you're trying clone object gets bit more complicated. can have @ jquery.extend (use or use how implement in own code).
note adding prototype break (add to) for (var in matrix4x4)
unless change of code:
for (var in m44){ if(m44.hasownproperty(i)){ console.log(i); } }
Comments
Post a Comment