Would some like Object.inspect(myObj) be useful?

Brendan Eich brendan at mozilla.com
Thu Jun 14 10:52:55 PDT 2012


Allen Wirfs-Brock wrote:
> On Jun 14, 2012, at 10:22 AM, Brendan Eich wrote:
>
>> Hemanth H.M wrote:
>>> var info = { name: "Hemanth", url: "http://h3manth.com/", location : "Earth", get : function() {} };
>>> Object.keys(info)
>>> ["name", "url", "location", "get"]
>>>
>>> Now, it's not clear about the 'type' of the keys; name is a String, where as get is a function.
>>>
>>> Compared to something like inspect module in python that give o/p like :
>>>
>>> ('__delslice__',<method-wrapper '__delslice__' of list object at 0x1005252d8>)
>> Python's repr convention is missed in JS, but instead of trying to force everything into a standard string representation, how about just building on ES5 (already mailed to you, sharing with list here):
>>
>> js>  o = {p:1, q:2, get r(){return 3}}
>> ({p:1, q:2, get r () {return 3;}})
>> js>  function inspect(o) {
>>   var a = [];
>>   Object.getOwnPropertyNames(o).forEach(function (k) {
>>     a.push(Object.getOwnPropertyDescriptor(o, k));
>
> You probably want to make the above line:
> a.[k] = Object.getOwnPropertyDescriptor(o,k}

Confusing .[ typo there :-P.

> This should give you a descriptor that can be passed back into Object.defineProperties
>
> Also initialize a to { }.

Let me redo the REPL session:

js> o = {p:1, q:2, get r(){return 3}}
({p:1, q:2, get r () {return 3;}})
js> function inspect(o) {
   var r = {};
   Object.getOwnPropertyNames(o).forEach(function (k) {
     r[k] = Object.getOwnPropertyDescriptor(o, k);
   });
   return r;
}
js> d = inspect(o)
({p:{configurable:true, enumerable:true, value:1, writable:true}, 
q:{configurable:true, enumerable:true, value:2, writable:true}, 
r:{configurable:true, enumerable:true, get:(function () {return 3;}), 
set:(void 0)}})

Thanks. Any reason this wasn't included in ES5 that you recall?

/be


More information about the es-discuss mailing list