javascript - Concatenating my object with a string -
this question objects in javascript.
i notice can add string , error object fine, , error object text concatenated no problems.
try { throw new error('sample text'); } catch (error) { document.writeln('there error. ' + error); // go plus sign! }
this outputs there error. error: sample text
pretty cool. error object knew string wanted concatenate. own objects not act nice this.
var myobject = (function () { var text = 'text want concat.', get_text = function () { return text; }, = {}; that.get_text = get_text; return that; }()); document.writeln('what object say: ' + myobject); // uncool
my own object outputs what object say: [object object]
, not act nice error object does.
i not want [object object]
output. how can change string myobject spits out when being added string?
you should give objects tostring
method returns appropriate string. need rename get_text
tostring
. write this:
var myobject = (function () { var text = 'text want concat.'; return { tostring : function () { return text } }; }());
Comments
Post a Comment