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

# Hemanth H.M (13 years ago)

Was just wondering if something like *Object.inspect(myObj) *would give all the attributes of that particular object.

# T.J. Crowder (13 years ago)

On 14 June 2012 10:42, Hemanth H.M <hemanth.hm at gmail.com> wrote:

Was just wondering if something like *Object.inspect(myObj) *would give all the attributes of that particular object.

Is there a proposal or strawman you're referring to? I'm not immediately seeing it, but I'm still new here. PrototypeJS has an Object.inspectapi.prototypejs.org/language/Object/inspectfunction,

but I don't see a strawman suggesting it join the specification.

-- T.J.

# Hemanth H.M (13 years ago)

No there is no proposal of that I'm aware of in Strawman, just asking the group if it's useful?

# David Bruant (13 years ago)

Le 14/06/2012 11:42, Hemanth H.M a écrit :

Was just wondering if something like *Object.inspect(myObj) *would give all the attributes of that particular object.

In ES5, there are Object.getOwnPropertyNames as well as Object.getOwnPropertyDescriptor. These are the lowest-level constructs you need to do any introspection, I think.

# T.J. Crowder (13 years ago)

On 14 June 2012 11:07, Hemanth H.M <hemanth.hm at gmail.com> wrote:

No there is no proposal of that I'm aware of in Strawman, just asking the group if it's useful?

I can't speak for the group, but I think you'd need to explain it a lot more. :-) What would it return? How does it relate to the existing reflect_api harmony proposalharmony:reflect_api?

How does it differ from something built using Object.keys or for..in? Does it include non-enumerable properties? When you say "attributes," do you mean properties or something else?

Not harshing, just trying to understand what you're raising.

(Side note: The convention on the list seems to be quote-first and comment below the quote [as above], rather than the other way around [which is gmail's default].)

-- T.J.

# Russell Leggett (13 years ago)

On Thu, Jun 14, 2012 at 5:42 AM, Hemanth H.M <hemanth.hm at gmail.com> wrote:

Was just wondering if something like *Object.inspect(myObj) *would give all the attributes of that particular object.

What would this function return? There's already an Object.keysdeveloper.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keyssfunction

  • does that do what you wanted? That's in ES5.
# Hemanth H.M (13 years ago)

The inspect module provides functions for introspecting on live objects and their source code.

# Allen Wirfs-Brock (13 years ago)

On Jun 14, 2012, at 9:49 AM, Hemanth H.M wrote:

The inspect module provides functions for introspecting on live objects and their source code.

As do many of the ES5 methods in Object.*. It's not clear what you mean by source code in this context.

If you want to have discussion about an idea like this you have to provide a much more complete description of what you have in mind.

# Hemanth H.M (13 years ago)

var info = { name: "Hemanth", url: "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>)

# Brendan Eich (13 years ago)

Hemanth H.M wrote:

var info = { name: "Hemanth", url: "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)); }); return a; } js> d = inspect(o) [{configurable:true, enumerable:true, value:1, writable:true}, {configurable:true, enumerable:true, value:2, writable:true}, {configurable:true, enumerable:true, get:(function () {return 3;}), set:(void 0)}]

ES5 has plural Object.defineProperties for defining more than one property on an object, and of course Object.create that defines all the properties on a new object from a descriptor-map. But it doesn't have anything like this inspect. We could add such an Object.getPropertyDescriptors API, for sure.

# Hemanth H.M (13 years ago)

WOW! Thanks a ton for the clarification.

# Brendan Eich (13 years ago)

Brendan Eich wrote:

ES5 has plural Object.defineProperties for defining more than one property on an object, and of course Object.create that defines all the properties on a new object from a descriptor-map. But it doesn't have anything like this inspect. We could add such an Object.getPropertyDescriptors API, for sure.

Object.getOwnPropertyDescriptors, I should have written. "Own", sigh.

# Allen Wirfs-Brock (13 years ago)

On Jun 14, 2012, at 10:22 AM, Brendan Eich wrote:

Hemanth H.M wrote:

var info = { name: "Hemanth", url: "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}

This should give you a descriptor that can be passed back into Object.defineProperties

Also initialize a to { }.

# Brendan Eich (13 years ago)

Allen Wirfs-Brock wrote:

On Jun 14, 2012, at 10:22 AM, Brendan Eich wrote:

Hemanth H.M wrote:

var info = { name: "Hemanth", url: "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?

# Allen Wirfs-Brock (13 years ago)

On Jun 14, 2012, at 10:52 AM, Brendan Eich wrote:

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

Minimalism. Object.* wasn't intended to be a comprehensive reflection library, just a set of essential primitives that could be used tin building such.

# Tom Van Cutsem (13 years ago)

2012/6/14 Allen Wirfs-Brock <allen at wirfs-brock.com>

On Jun 14, 2012, at 10:52 AM, Brendan Eich wrote:

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

Minimalism. Object.* wasn't intended to be a comprehensive reflection library, just a set of essential primitives that could be used tin building such.

I think that was a good choice. There are many utility functions one may want to build on top of ES5's set of primitives. For example, traits.js was my attempt at providing a minimal number of utilities to treat property descriptor maps as composable traits. (The above inspect function is exactly traits.js's Object.getOwnProperties which lifts an object literal into a property descriptor map, cf. < soft.vub.ac.be/~tvcutsem/traitsjs/api.html>)