0

Looking at a test code with linked lists in JavaScript below, the input parameters of the addTwoNumbers() function are objects. The actual object is shown when I stringified the parameter. Instead, a simple console.log() shows something that looks like an array but is surely not. The question here is why the simple console.log() didn't output the actual object as it should, i.e. https://onecompiler.com/javascript/3z5eafms5.

Am I missing something or is it just the platform that reforms the output in some way?

enter image description here

InSync
  • 4,851
  • 4
  • 8
  • 30
Maverick
  • 1,105
  • 12
  • 41
  • Does this answer your question? [Is Chrome’s JavaScript console lazy about evaluating objects?](https://stackoverflow.com/questions/4057440/is-chrome-s-javascript-console-lazy-about-evaluating-objects) – Konrad Apr 12 '23 at 07:15
  • @Konrad Hi, thanks, no its a different issue. – Maverick Apr 12 '23 at 07:18
  • [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) And a proper [mre] of your issue belongs directly into your question (and not just dumped onto an external platform.) – CBroe Apr 12 '23 at 07:27

1 Answers1

0

ListNode objects have a Symbol(nodejs.util.inspect.custom) method that is used by NodeJS's console.log()/util.inspect() to retrieve a representational object:

const symbol = require('util').inspect.custom;
// or const symbol = Symbol.for('nodejs.util.inspect.custom')

console.log(Object.getPrototypeOf(l1));
// { [Symbol(nodejs.util.inspect.custom)]: [Function (anonymous)] }

console.log(typeof symbol, symbol in l1);
// 'symbol', true

class Foo {
  constructor() {
    this.foo = 42;
  }
  [symbol]() {
    return [43];
  }
}

console.log(new Foo());
// [43]
InSync
  • 4,851
  • 4
  • 8
  • 30