1

is it possible to create your own condition/control flow syntax in js? for example:

when( condition ) {
 // execute code
}

this would add some sort of listener to a variable/object and execute whenever the condition is true.

I may just have to create my own programming language.

2
  • 4
    Short answer: no. Slightly longer answer that actually solves your problem: use an Observable. github.com/Reactive-Extensions/RxJS Commented Feb 21, 2017 at 16:27
  • I may just have to create my own programming language. this sounds hard and javascript IMHO is not your best choice.... Commented Feb 21, 2017 at 16:30

2 Answers 2

1

This is actually two questions:

  1. Can syntax be added to JavaScript directly?
  2. Can I set up code that runs when a condition changes?

The answer to the first is no. You can use preprocessing like sweet.js macros to accomplish that but its non-trivial.

The answer to 2 is yes, you can accomplish this in any ES 5 compliant environment (IE 9+):

var condition = {val: null};
Object.defineProperty(condition, "isTrue", {
  set: function(val) {
    if (val && !this.val) {
      runSomeCodeYouWantRun();
    }
    this.val = val;
  },
  get: function() {
    return this.val;
  }
});

So whenever any code changes condition.isTrue the specified code will be run if the change is truthy. But for situations like this I prefer less ad-hoc approach. ES 2015 Proxy traps make this much cleaner (IMHO) but support isn't quite there yet. What you really are looking for here to get the job done today is an Observable.

Also note that writing a language to solve a problem like this is roughly equivalent to building a car out of spare parts to drive to the store for groceries. Just buy a car.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I was thinking of an idea for a JS framework that would help abstract away typical processes and help by providing a convenience in many situations. It would be a combo of MVC like Angular and a library like jQuery that somehow works together.
@ryanwaite28 check out aurelia. But that proves the point again, always check for an existing solution first as they benefit from corporate backing and/or lots of eyes/real world usage/etc.
Thanks! it's hard to invent or innovate nowadays
1

Welcome to the wonderful world of JavaScript transpilers.

JavaScript in and of itself does not have any utilities for creating your own syntax. In response to this, many tools exist online and in the NPM repositories that add just this sort of feature to JS by translating it to browser-compatible JavaScript. Here's a small sampling:

  • Babel.js adds ES6 syntax to ES5 JavaScript
  • Browserify adds Node.JS's require() functionality
  • Uglify.JS compresses JS into the smallest form possible that will still execute the same way
  • TypeScript (while more technically its own language) adds static type-checking to JavaScript

All of these transpilers, however different, all work the same way: they parse the source file(s) to an abstract syntax tree (or AST), run some transformations on that tree, and then spit out the resulting JavaScript file. If you wanted to create your own special syntax in JavaScript, you would do more or less the same thing. (Uglify.JS stands out as being particularly customizable in this regard).


All of that said, none of these transpilers fundamentally change the way that JavaScript works - you still have to translate whatever fancy syntax you want to use into plain, browser-executable JavaScript. This means that although you will be able to write your when(){} block syntax, the expression inside the parentheses cannot be a simple Boolean expression, but must involve something like an Observable object that inserts a callback (Knockout.JS does have a tool for doing this out of an expression built from its Observables).

1 Comment

Thanks. I was thinking of an idea for a JS framework that would help abstract away typical processes and help by providing a convenience in many situations. It would be a combo of MVC like Angular and a library like jQuery that somehow works together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.