0

For tasks that are done very often, e.g. console.log, I would like to create a reserved word.

Yes, I could create a utility function, but then I'd have to import it into every file where I would like to call it.

When I say reserved word, I mean the same way that debugger is a reserved word.

How would you create a custom reserved word in javascript?

zero_cool
  • 3,960
  • 5
  • 39
  • 54
  • 4
    Rewrite V8 or any other engine. – Jonas Wilms Jul 16 '18 at 17:59
  • I am also thinking it is changing something core - but wondering if there is a simpler way than rewriting the entire engine :) – zero_cool Jul 16 '18 at 18:00
  • 3
    As Jonas mentioned, there isn't really any support for that short of rewriting something in the engine itself. This may be an XY problem. What are you trying to solve? – samanime Jul 16 '18 at 18:01
  • You could use a getter inside of a `with` statement to get a behaviour like the `debugger` statement, but thats still far away from any real world usage. – Jonas Wilms Jul 16 '18 at 18:03
  • 1
    I run a snipplet when I need to run code, seems like an easy thing to do.... – epascarello Jul 16 '18 at 18:03
  • Just shorthand things that you do very often. I do a lot of work in React so having console.log, this.props, and this.state as cl(tp, ts) would be awesome. – zero_cool Jul 16 '18 at 18:03
  • @epascarello what do you mean by snipplet? – zero_cool Jul 16 '18 at 18:04
  • 1
    Aha ... So you just want to add some shortcuts to your editor? Thats probably way easier then a "reserved word" – Jonas Wilms Jul 16 '18 at 18:04
  • 2
    You use a transpiler or something like https://www.sweetjs.org/. You wouldn't be able to still call the resulting language "JavaScript", though. – Bergi Jul 16 '18 at 18:05
  • 1
    https://developers.google.com/web/updates/2015/05/predefined-snippets – epascarello Jul 16 '18 at 18:05
  • @JonasW. I wanted the entire team to be able to use this, and maybe the community at large if others found it useful. If it's possible to write a little npm module that would hook into webpack somehow and allow it's use without having to import it into every file you're working in, that would be amazing. Or maybe an atom plugin. – zero_cool Jul 16 '18 at 18:10
  • what about something like creating your own emmet actions: https://docs.emmet.io/customization/ – ardev Jul 16 '18 at 18:10
  • @ardev I'll take a look at emmet. – zero_cool Jul 16 '18 at 18:11

0 Answers0