Console : Printing objects with more details
Hi everyone, I started scripting for after effects 3 days ago and I came across tons of disappointments.
I think the $.write() and $.writeln methods don't do enough. (I'm used to console.log in web development). My biggest problem right now is that if you log an object, it'll only output [object Object].
I'm trying to print the full object with all its properties and sub/sub_sub... objects.
Question #1: Am I missing something? Is there an easy way to do this?
Question #2: if #1's answer is no, do you guys have an idea why I can't print the properties of the sub-objects using this method. (I recommend you guys try it)
var testObj = {
property_string : "string",
property_int : 999,
property_arr : ["test",1],
property_obj : {
sub_property_string : "hello"
}
};
objLog(testObj);
function objLog(object){
var log = object.toString() + " {";
var line;
for (var property in object) {
line = "\r" + tabsString(1) + property.toString() + " : " + object[property].toString();
log += line;
objLoop(property,object);
}
log += "\r }"
$.writeln(log);
/*
* Creates Tabs for the formatting
* */
function tabsString(amount){
var string = "";
for(var i = 0; i < amount; i++){
string += " ";
}
return string;
};
/*
* Should be an infinite-able recursive loop to always print the properties of any object at any level.
* */
function objLoop(prop,obj){
var subObject = obj[prop].toString();
if(subObject.indexOf("[object Object]") !== -1){
line = "{\r"
log += line;
for (var subProperty in subObject){
/*
* Can't get anything to work in this part.
* Any object contained in an object won't print it's properties
* */
line = tabsString(2) + "*Should be printing propeties here*"
log += line;
line = subProperty + "\r" + subObject.toString() + " \r";
line = "\r" + tabsString(2) + subProperty.toString() + " : " + subObject[subProperty].toString();
log += line;
}
log += "\r" + tabsString(2) + "}"
}
};
}
Currently, this will output the following in the javascript console:
[object Object] {
property_string : string
property_int : 999
property_arr : test,1
property_obj : [object Object]{
*Should be printing propeties here*
toJSON :
function () {
return this.valueOf();
}
}
}
I'm really confused on why it prints
"toJson : function(){ return this.valueOf(); }"
instead of
sub_property_string : hello
Anyway, I hope you guys have better ways than me to print lots of information in the console because I really feel like a newbie.
Thanks a lot!
