4

The following is my block of code. since I haven't installed Firebug in IE, Every time when I try to test my code in IE I'm getting an error message console is undefined. so I decided and developed this block of code, so that console.log work only in firefox and to avoid error messages in IE.

function clog() {
 if(window.console && window.console.firebug) {
 var a =[];
 for(var i=0;i<arguments.length;i++) {
 a.push(arguments[i]);
 }
 console.log(a.join(' , '));
 }
}

my code is working fine and I'm getting the results which I wanted,

but when I tried to use the above code on jQuery ( for example clog($('body')); ),

the result which I expected is to be jQuery(body) . but I'm getting the result as [object Object]

How can I get The results which I expected ?

Thank you !!

asked Jul 24, 2010 at 11:19

3 Answers 3

4

When you call a selector like that, say $('body') what you're doing is creating an object, a jQuery object...so your output is correct.

If you want to display something other than it's .toString(), then you should call that property, for example:

$('body').selector //body
$('body').length //1
$('body').context //document

If all you're using is console.log, I find just creating it if it's missing (as opposed to checking whenever you want to use it) is much easier, just have this run before any of your logging code:

if (typeof console == "undefined") console = { log: function () { } };

Then you can remove your current if check.

answered Jul 24, 2010 at 11:21
Sign up to request clarification or add additional context in comments.

10 Comments

To be more correct: it's called a JavaScript object, not a jQuery object. ;)
@Marcel - it's both...it's literally an instance of a jQuery object, like an Array is a JavaScript object, but specifically it's an Array :)
Why doesn't if(window.console) work correctly? It will evaluate to false if console is not available.
@Marcel - Not in IE...had this issue yesterday, don't ask me why, even in IE8 this still breaks, haven't tested in 9 though.
Ah, jQuery extends the default object? I'm not a jQuery expert, I merely thought it returned the getElementById object.
|
3

I always write a wrapper function (to keep non 'console' browers from having problem)

function log(msg) {
 try {
 console.log(msg);
 } catch(e){}
}

You could examine the "msg" object, then check the type to determine whether it's a "jQuery" object, and extract the data.

answered Jul 24, 2010 at 11:30

Comments

2
console.log(a);

instead of

console.log(a.join(' , '));

should do it.

Array.prototype.join will concatenate all array entrys into a String. That means

var b = [{}, "test"];
b.toString()

will evaluate to "[object Object],test" regardless what methods or members are within that object. You just lose that information calling .toString().

answered Jul 24, 2010 at 11:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.