JSFiddle - React, Tailwind, and code Playground
HTML
<span id="test"></span>
JavaScript
function evalInContext(source, context) {
source = '(function(' + Object.keys(context).join(', ') + ') {' + source + '})';
var compiled = eval(source);
return compiled.apply(context, values());
// you likely don't need this - use underscore, jQuery, etc
function values() {
var result = [];
for (var property in context)
if (context.hasOwnProperty(property))
result.push(context[property]);
return result;
}
}
evalInContext('$("#test").append(value)', { value: 'Hello, world!<br/>' });
evalInContext('$("#test").append(this.value)', { value: 'Hello, again...<br/>' });
// hide the "window" global instance
evalInContext('$("#test").append(window.toString())', { window: { } });