0

我正在寻找一种方法来公开未知 JavaScript 对象的类型和方法。

我正在实现第三方代码,其方法的文档有限,因此我无权访问任何可以告诉我应该在response.

有没有办法确定这是什么类型的对象并公开它包含的方法?

for (var i = 0; i <= response.length - 1; i++) {
     console.log(response.i);
}

Firebug 控制台中的输出:

响应:[对象对象],[对象对象],[对象对象],....

4

4 回答 4

2

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.

于 2012-09-19T12:33:27.227 回答
1

你可以使用

  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

于 2012-09-19T12:23:34.353 回答
0

您可以将其作为属性添加到windowfirebug 中并使用 DOM 浏览器,它允许您检查整个对象树。

于 2012-09-19T12:24:41.623 回答
0

如果您想亲自观看:

  1. 打开 Chrome 检查器 -> Web 控制台
  2. console.log(response);
  3. Enjoy, there you can view all properties recursively with details
于 2012-09-19T12:26:05.210 回答