Categories
interview

Using object as key in Js objects

/* var Tree = function(val) {
   this.val = val;
}; */

class Tree {
  constructor(val) {
    this.val = val;
    this.left = this.right = null;
  }
}

const obj = {};

const treeNode = new Tree(10);

obj[treeNode] = 'Tree Val: ' + treeNode.val;

console.log(obj[treeNode]);
// Output "Tree Val: 10"

Demo