1
  • I'm trying to create a simulator for console.log to use it in my phone to show deferant datatype result, code errors - the code is working fine and display, highlight all datatypes

enter image description here

  • but i want also to display constcrutor, scopes, prototypes like the web console is there's a way to loop throw them and get their data like this image

enter image description here

JS_INF
  • 467
  • 3
  • 10
  • 1
    [this](http://ecma-international.org/ecma-262/5.1/#sec-8.6.2) link may help you – Raz Luvaton Aug 15 '21 at 00:12
  • See also [Programmatically accessing `[[Scopes]]`](https://stackoverflow.com/q/52150979/1048572), [What is `[[Scopes]]`](https://stackoverflow.com/q/45506745/1048572) and [How to get the scope from where a function was called?](https://stackoverflow.com/q/45313383/1048572) – Bergi Aug 16 '21 at 08:40

1 Answers1

2

The constructor property can be gotten by referencing it explicitly, or by iterating over all properties of the object (not just enumerable properties), with Object.getOwnPropertyNames.

The [[Prototype]] property is equivalent to the value returned by Object.getPrototypeOf, so you can call that to access the prototype object and iterate through it too, if you want.

Perhaps something like:

const recursiveLogAllProperties = (obj) => {
  Object.getOwnPropertyNames(obj).forEach(prop => console.log(prop, obj[prop]));
  const proto = Object.getPrototypeOf(obj);
  if (proto === Object.prototype) return;
  console.log('------ Prototype:');
  recursiveLogAllProperties(proto);
}
const arr = [1, 2];
recursiveLogAllProperties(arr);

The [[Scopes]] internal property is not accessible from JavaScript.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320