Javascript `eval()` scope in ES6
<p>I am dynamically creating a function on a <code>Foo1</code> instance named <code>test</code>. I am creating this function using <code>eval</code>. I would expect that this function would have access to the <code>Foo2</code> class, however I am getting a <code>ReferenceError: Foo2 is not defined</code>.</p>

<p>I have opened up an issue with <code>Babel</code> about this and can be be found <a href="https://phabricator.babeljs.io/T7526?workflow=create#R" rel="nofollow">here</a></p>

<p>If you would like to run the example code yourself, download it from <a href="https://github.com/fbgrecojr/foo" rel="nofollow">here</a> and follow the instructions in the <code>README.MD</code> to reproduce.</p>

<p>To run:</p>

<pre>
npm install
npm run start
naviage to localhost:8080
</pre>

<p>Here is the directory structure for my Minimal, Complete, and Verifiable example in my environment:</p>

<pre><code>root
  - src
    - Foo1.js
    - Foo2.js
  - .babelrc
  - app.js
  - package.json
</code></pre>

<p>Foo1.js</p>

<pre><code>import Foo2 from './Foo2.js';

export default class Foo1 {
    constructor() {
        // Isolate the impact of eval within makeFunction
        let makeFunction = text =&gt; {
            return eval("(function() { " + text + "})");
        };
        this.test = makeFunction('let foo2 = new Foo2(); foo2.test();');
    }
}
</code></pre>

<p>Foo2.js</p>

<pre><code>export default class Foo2 {

    test() {
        console.log('i\'m working!');
    }

}
</code></pre>

<p>.babelrc</p>

<pre><code>{
  "presets": ["es2015"]
}
</code></pre>

<p>app.js</p>

<pre><code>import express from 'express';
import http from 'http';
import Foo1 from './src/Foo1.js';

const app = express();
const server = http.createServer(app);

app.get('/', (req, res) =&gt; {
    let test = new Foo1();
    test.test();

    res.end('bye');
});

server.listen(8080);
</code></pre>

<p>package.json</p>

<pre><code>{
    "name": "test",
    "scripts": {
        "start": "./node_modules/babel-cli/bin/babel-node.js ./app.js"
    },
    "dependencies": {
        "http": "*",
        "express": "*",
        "babel-cli": "^6.7.7",
        "babel-core": "^6.7.7",
        "babel-polyfill": "^6.3.14",
        "babel-preset-es2015": "^6.6.0"
    }
}
</code></pre>

<p>Now, If I change the <code>Foo2.js</code> class to the previous version of javascript everything works like a charm:</p>

<pre><code>function Foo2() { }

Foo2.prototype.test = function() {
    console.log('i\'m working!');
};

module.exports = Foo2;
</code></pre>