I connected a library to the javaScript project, and create one of the objects in this library. But I don’t know what this object has methods and properties (I don’t know what they are called), there is almost no documentation, but the library is big and I don’t want to dig into its code.

Tell me, please, is it possible to learn the names of properties and methods from the created Object via the browser console, or by any other means and methods?

    5 answers 5

    http://javascript.ru/for..in

      As already mentioned above, the simplest and most obvious way is for..in. Let's say you have an object:

      var obj = { int : 10, str : 'qwerty', bool : true, func : function() { alert(this.int); } }; 

      You can run by its properties as follows:

       for(var i in obj) console.log('obj[' + i + '] = ' + obj[i]); 

      But it is quite possible that your obj will have some inherited properties. For example:

       Object.prototype.Foo = 'foo'; 

      In this case, the previous code will also output the Foo property. To get only those properties that belong specifically to this object, you need to do this:

       for(var i in obj) if(obj.hasOwnProperty(i)) console.log('obj[' + i + '] = ' + obj[i]); 

      And finally, it should be said that besides the good old

       console.log(obj) 

      for arrays, you can use the method

       console.table() 

      True, the standard is not, and therefore does not work in all browsers (of course, it is not in the donkey)

         console.log(yourObject); 
           var o = { x: 5, y: 4 } console.log(Object.keys(o)); // ["x", "y"] 

            The above code will display all the keys and the property of the object.

             for (var key in object) { console.log(key, object.key); } 

            • How does this answer differ from two adjacent answers? in which exactly the same advise to use for..in ? - Grundy