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

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -

delphi - How to convert bitmaps to video? -