Knowing Javascript Object Type,Properties,Functions
Javascript being a dynamically typed language, sometimes it helps to know the properties and functions of a given object. Anyone who has worked with javascript in an Object Oriented way would testify this. Here is a snippet of javascript code that would give the Type, member functions and properties of a given object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function getJsObjectInfo(theObject){ var properties = ""; var functions = ""; // Loop through the properties/functions for (var member in theObject) { // Check if it’s a function else member if (theObject[member] instanceof Function) { functions += member + ", "; }else{ properties += member + ", "; } } var clsName = theObject.constructor.toString(); var start = clsName.indexOf('function ') + 9; var stop = clsName.indexOf('('); clsName = clsName.substring(start, stop); var display = "The object is of type " + clsName + "\n" +"Methods : " + functions + "\n" + "Propreties : " + properties; alert(display); } |