JSFiddle - React, Tailwind, and code Playground

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/12.3.1/math.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/12.3.1/math.min.js"></script>

JavaScript

console.log("mathjs version", math.version);

class MapScope {
  constructor () {
    this.localScope = new Map()
  }

  get (key) {
    // Remember to sanitize your inputs, or use
    // a datastructure that isn't a footgun.
    return this.localScope.get(key)
  }

  set (key, value) {
    return this.localScope.set(key, value)
  }

  has (key) {
    return this.localScope.has(key)
  }

  keys () {
    return this.localScope.keys()
  }
  
  createSubScope() {
  	return this;
	}
  
  customMethod() {
  	return Math.random();
  }
}

const scope = new MapScope();
scope.set("x", 2);

const customFunction = (args, mathjs, scope) => {
  return scope.get(args[0].name) * 2 * scope.customMethod();
};
customFunction.rawArgs = true;

math.import({ customFunction })

console.log(math.evaluate('customFunction(x)', scope));