Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[inline] Support for Object Methods #168

Open
kungfooman opened this issue Apr 17, 2024 · 0 comments
Open

[inline] Support for Object Methods #168

kungfooman opened this issue Apr 17, 2024 · 0 comments

Comments

@kungfooman
Copy link
Contributor

Not everyone knows this function-in-object-shorthand trick (known as an ObjectMethod in e.g. Babel AST), but I like it:

const methods = {
  mul(a, b) {
    return a * b;
  },
  pow(a, b) {
    return a ** b;
  },
};
const R201 = Algebra(2,0,1);
const mul = R201.inline(methods.mul);
console.log(mul);

However, it doesn't work right now. A workaround for the inline function:

  var txt = (intxt instanceof Function) ? intxt.toString() : `function(){return (${intxt})}`;
  // Handle methods in object notation like: {mul(a, b) {return a * b;}}
  if (!(txt.startsWith('(') || txt.startsWith('function'))) {
    txt = 'function ' + txt;
  }

Currently it just fails:

Uncaught SyntaxError: Unexpected token '{'
at Element.inline (ganja.js:1852:136)
    at <anonymous>:10:18

Demo: https://enki.ws/ganja.js/examples/coffeeshop.html#NG7DYcbaq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant