views:

69

answers:

5
+1  Q: 

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?

thanks

A: 

Something like...

alert(n.name); 

...I think is what you want.

If you are trying to debug, you would be better suited to using FireFox/Firebug instead of inserting a load of alerts();

StingyJack
A: 

There are a couple of alternatives:

1. Use http://www.gscottolson.com/blackbirdjs/
2. Use console.log() that comes with Firebug, but requires Firefox (even if you only target only IEs, it's still wise to use Firefox and Firebug as aprt of testing of your web app development)
Khnle
3. Use log4javascript (http://log4javascript.org/), which is a more fully featured logging library than Blackbird.
Tim Down
+1  A: 

It depends what you mean by alerting the complete object.

You can't really just output every object as a string and have it make sense. To define how an object will display itself as a string we use the .toString(); method.

So try alert(n.toString()); and see if that will give you what you want. If the object is your own, then define the toString(); and have it return a string of the parameters and fields that you want to output.

Paul
Even if the object isn't your own, you can add a helpful `toString` method.
Paul Butcher
It's actually the `toString()` methods that *is* being called now, but the one inherited from the objects prototype outputs "[object]". He needs to override this one.
Sean Kinsey
+2  A: 

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.

Mike Clark
A: 

I like the var_dump in php, so I often use a function like this to dump variables

function var_dump(object, returnString) { var returning = ''; for(var element in object) { var elem = object[element]; if(typeof elem == 'object') { elem = var_dump(object[element], true); } returning += element + ': ' + elem + '\n'; } if(returning == '') { returning = 'Empty object'; } if(returnString === true) { return returning; } alert(returning); }

Gabriel