I have simple JavScript REPL:
function(command) {
if (command !== '') {
try {
var result = window.eval(command);
if (result) {
if (result instanceof $.fn.init) {
this.echo(`#<jQuery>`);
} else if (typeof result === 'object') {
var name = result.constructor.name;
if (name) {
this.echo(`#<${name}>`);
} else {
this.echo(result.toString());
}
} else {
this.echo(new String(result));
}
}
} catch(e) {
this.error(new String(e));
}
}
}
But it doesn't work with let or const:
js> let x = 10
js> x
ReferenceError: x is not defined
js>
Is there a hack that will make this work? I have no idea how Google Chrome Dev Tools make it work. It will probably take a long time to read the code. Just want to ask before I will possibly spend a lot of time reading it's code.
I know that I can just replace let with var before evaluation, but I don't like this solution.
EDIT: the answers to duplicated question only show why let and const doesn't work in eval, but don't answer how to make eval work with let or const, like in Dev Tools. My question is: how to make eval work with let and const.