expose unknown JavaScript object methods from third-party code
-
02-07-2021 - |
题
I am looking for a way to expose the type and methods of an unknown JavaScript object.
I am implementing third-party code that has limited documentation for their methods, so I do not have access to any documentation that would tell me what sort of object I should expect in response
.
Is there any way to ascertain what type of object this is and expose the methods it contains?
for (var i = 0; i <= response.length - 1; i++) {
console.log(response.i);
}
The output in the Firebug Console:
response: [object Object],[object Object],[object Object],....
解决方案
You may want to try
console.dir(response);
It lists all the object's properties, and their respective types are indicated by colorization:
- red key: Constructor functions
- green key: methods
- black key: other
- red value: string
- green value: object
- blue value: boolean
- gray value:
undefined
/null
This list is not exhaustive, and I haven't found any documentation on it.
其他提示
You can use for in
function printProperties(response, path){
path = path || "";
for (var prop in response) {
if(typeof response[prop] == 'object'){
printProperties(response[prop], path + prop + ".");
continue;
}
console(path + prop + " = " + response[prop]);
}
}
jsfiddle
You can add it as a property to window
and the use the DOM browser in firebug, it allows you to examine the entire object tree.
If you want to see it just by your self:
- Open Chrome Inspector -> Web Console
console.log(response);
- Enjoy, there you can view all properties recursively with details