title: Nested classes instances in Javascript/Node.js
body: <p>I'm using puppeteer which is a NodeJS module that controls chrome. </p>
  
  <p>It has 2 functions to initiate a new browser and a new page. </p>
  
  <p>const browser = await puppeteer.launch() and browser.newPage()</p>
  
  <p>I want to create a class for creating a new page and new browser.</p>
  
  <p>This is the old way I was doing it, without classes, it works but it doesn't allow me to create new pages. This is why I want to move to using classes.</p>
  
  <pre><code>let chrome = {}
  
  chrome.init = async (options) =&gt; {
      chrome.browser = await puppeteer.launch(options)
      chrome.page = await chrome.browser.newPage()   
  }
  
  chrome.pageContains = async (string) =&gt; {
  
      return await chrome.page.evaluate( (string) =&gt; {
  
          const regex = new RegExp(string, 'i')
  
          return regex.test( document.querySelector('body').innerText )
  
      }, string)
  }
  
  module.exports = chrome
  </code></pre>
  
  <p>Here's my new code but I have no idea what I'm doing, it's obviously wrong and makes no sense.</p>
  
  <pre><code>chrome.init = async (options) =&gt; {
      return {browser: await new Chrome(options)
  }
  
  class Chrome {
      async constructor(options) {
          this.browser = await puppeteer.launch(options)
      }
  
      newPage() {
          return await this.browser.newPage()
      }
  }
  
  class Page {
      async constructor() {
          this.page = await chrome.browser.newPage()
      }
  
  }
  </code></pre>
  
  <p>So how do I make my old code work using classes instead of an object?</p>