Search code examples
javascriptdom-events

Alert the object


I am setting an object like this:

n.name = n.name.join(String.fromCharCode(255));
n.description = n.description.join(String.fromCharCode(255));

I want to be able to alert(n); but it tells me [Object]

Is there a way to alert complete object?


Solution

  • Javascript supports adding a toString() function to your objects. This will get called when you alert your object. For example:

    n.toString = function(){
        return this.name + '\n' + this.description;
    }
    

    then alert(n); will display whatever content your function specifies.