2

When I load a script as simple as:

class ClassName {
  constructor() {
    this.x = 3;
  }
}

into a web page as text/paperscript, the web console returns

SyntaxError: Unexpected token (1:6)

If I switch to text/javascript everything runs as intended, meaning there's no syntax error. Ah, and of course I've linked the paper.js library in the HTML source.

Why JavaScript classes don't work in paperscript? Isn't paperscript just an extension of JavaScript?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 1
    I would be very surprised to learn that any browser understands the MIME type "text/paperscript" at all. I guess it's possible that a library for that tool would scan the document for ` – Pointy Feb 25 '20 at 21:35
  • 1
    A library can register to process such things. That's how babel works, if used in the browser. It registers itself as a script handler. Paperjs does this. Or is supposed to. –  Feb 25 '20 at 21:37
  • In PaperJS, it's handled [here](https://github.com/paperjs/paper.js/blob/0a56c7cef09f614f6c5e540049fa980a1ee78570/src/core/PaperScript.js#L573) –  Feb 25 '20 at 21:37
  • Are you including the paper.js script before your script? Are you using the full script, or core? –  Feb 25 '20 at 21:38
  • @Amy right, that's pretty much exactly what I said :) Anyway this seems to require that a different JavaScript parser be used, [as mentioned in this Github issue](https://github.com/paperjs/paper.js/issues/1357) – Pointy Feb 25 '20 at 21:39
  • 1
    @Pointy I think what happened was, I started my comment while you were editing your own. –  Feb 25 '20 at 21:40
  • No worries :) [Here's another issue](https://github.com/paperjs/paper.js/issues/1183) that describes how to load an updated Acorn version. – Pointy Feb 25 '20 at 21:41
  • It's kind-of weird that they haven't made the standard distribution use a new parser, that first bug is from 2016. – Pointy Feb 25 '20 at 21:42
  • I've included the `paper.js` library as the first – user12962501 Feb 25 '20 at 21:55
  • Does this answer your question? [Paperjs causing class syntax error when not using Paperjs code](https://stackoverflow.com/questions/58847325/paperjs-causing-class-syntax-error-when-not-using-paperjs-code) – sasensi Feb 27 '20 at 08:55
  • It does, thank you! – user12962501 Feb 29 '20 at 14:13

0 Answers0