4

I'm using Uint8Array. I'm not used to using Uint8Array.

If this were Python:

>>> a = [1, 2, 3]
>>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> help(a.pop)
< ... shows helpful function documentation ... >

But I'm using the node shell:

$ node
> a = new Uint8Array([1, 2, 3])
Uint8Array { '0': 1, '1': 2, '2': 3 }
> a
Uint8Array { '0': 1, '1': 2, '2': 3 }
> a.pop
undefined
> help(a)
ReferenceError: help is not defined
    ...
> dir(a)
ReferenceError: dir is not defined
    ...    

Hmm, StackOverflow suggests Object.keys:

> Object.keys(a)  
[ '0', '1', '2' ]
> ???
... ???
... CTRL+D
$ 

Guess not!

Is there any equivalent way to inspect objects like this in node's shell environment?

Community
  • 1
  • 1
Claudiu
  • 224,032
  • 165
  • 485
  • 680
  • What about `dir()` without an argument, e.g. `locals()` and `globals()`? – Tomasz Gandor Jan 20 '21 at 15:50
  • OK, pressing TAB twice in the REPL does many interesting things (also on an empty prompt). Of course that is when working interactively, but `dir()` isn't a really a programmatic utility either. – Tomasz Gandor Jan 20 '21 at 15:55

2 Answers2

4

Press TAB after the .:

> a = new Uint8Array([1, 2, 3])
Uint8Array { '0': 1, '1': 2, '2': 3 }
> a. <<TAB>>
a.__defineGetter__      a.__defineSetter__      a.__lookupGetter__      a.__lookupSetter__      a.__proto__             a.constructor
a.hasOwnProperty        a.isPrototypeOf         a.propertyIsEnumerable  a.toLocaleString        a.toString              a.valueOf

a.BYTES_PER_ELEMENT     a.buffer                a.byteLength            a.byteOffset            a.copyWithin            a.entries
a.every                 a.fill                  a.filter                a.find                  a.findIndex             a.forEach
a.indexOf               a.join                  a.keys                  a.lastIndexOf           a.length                a.map
a.reduce                a.reduceRight           a.reverse               a.set                   a.slice                 a.some
a.sort                  a.subarray              a.values

> a.
Claudiu
  • 224,032
  • 165
  • 485
  • 680
0

It's longer to write on the console than python to get the functional equivalent

> a = new Uint8Array([1, 2, 3])
Object.getOwnPropertyNames(a)
[ '0', '1', '2' ]
> //get the properties of Object Type
> Object.getOwnPropertyNames(Object.getPrototypeOf(a))
[ 'constructor', 'BYTES_PER_ELEMENT' ]
> //get the properties of parent
> Object.getOwnPropertyNames(Object.getPrototypeOf(Object.getPrototypeOf(a)))
[ 'constructor', 'buffer', ...]

You could write a recursive function to get everything (if you didn't want to use a.[TAB])

function dir(obj, props) {
    if(typeof(props) === "undefined") { props = [];}
    //get parent object
    var pobj = Object.getPrototypeOf(obj);
    if(pobj) { 
        props = props.concat(Object.getOwnPropertyNames(pobj));
        return dir( pobj, props ) 
    }
    else 
    {
       return props.sort()
    }
}

> dir(a)
[ 'BYTES_PER_ELEMENT',
  '__defineGetter__',
  '__defineSetter__',
  '__lookupGetter__',
  '__lookupSetter__',
  '__proto__',
  'buffer', ...]
uosjead
  • 426
  • 6
  • 5