How to inspect Javascript Objects

后端 未结 8 537
半阙折子戏
半阙折子戏 2020-12-07 10:31

How can I inspect an Object in an alert box? Normally alerting an Object just throws the nodename:

alert(document);

But I want to get the p

8条回答
  •  抹茶落季
    2020-12-07 10:57

    There are few methods :

     1. typeof tells you which one of the 6 javascript types is the object. 
     2. instanceof tells you if the object is an instance of another object.
     3. List properties with for(var k in obj)
     4. Object.getOwnPropertyNames( anObjectToInspect ) 
     5. Object.getPrototypeOf( anObject )
     6. anObject.hasOwnProperty(aProperty) 
    

    In a console context, sometimes the .constructor or .prototype maybe useful:

    console.log(anObject.constructor ); 
    console.log(anObject.prototype ) ; 
    

提交回复
热议问题