-1

As crazy how it sounds, how do we give 'commas' an alias in javascript?

for example when we have something like this:

Normal version: functionCall(send, country);

Alias version: functionCall(send TO country);

The above is just an example and I am very curious on how I could achieve something like this. I got inspired by PHP sql queries.

This may seem redundant but it does help readability and gives more meaning to the comma depending on the context.

EDIT: Working with regex could do the job I guess.

Asperger
  • 3,064
  • 8
  • 52
  • 100
  • 2
    that will make your code *less* readable to people who know javascript well. – Daniel A. White Oct 04 '15 at 18:38
  • @DanielA.White I agree, regardless im extremely interested in how this would work. – Asperger Oct 04 '15 at 18:39
  • 4
    you could have the code as a string, then regex replace it, then eval it which all is a unmaintanable mess. – Daniel A. White Oct 04 '15 at 18:39
  • @DanielA.White that is what I thought. String then replace it as a non string character – Asperger Oct 04 '15 at 18:42
  • 4
    There are existing examples of customized syntax for JavaScript in the form of transcompilers. [Babel](http://babeljs.io/), [CoffeeScript](http://coffeescript.org/), [TypeScript](http://www.typescriptlang.org/), etc. You can certainly define your own to accomplish this, though it won't necessarily be a small task. – Jonathan Lonowski Oct 04 '15 at 18:43

3 Answers3

2

As others stated in the comments, regexing your entire script would lead to an utter and unmaintanable mess (and eval'ing to an endless security hell). But since you are talking about readability, how about sticking to the rules of Javascript and give your argument names more meaning:

function functionCall(send, toCountry) { ... }

But if you insist to give your script a more custom feeling, go for a DSL-like approach, so that you can work with

parcel.cardboardColor('brown').deliverPriority('high').toCountry('US').send();

For such an approach, you can find lots of resources on the net, like https://blog.jcoglan.com/2008/03/21/composing-dsls-in-javascript/

wiesion
  • 2,349
  • 12
  • 21
1

If you're doing mad science there, you can use the eval function and replace the code before you use it.

Here is an example:

var str = "function foo(send TO country) { alert(send + ' ' + country); }";
str += "foo('hello', 'world')"

function execute(input) {
  return eval(input.replace(/ TO /g, ", "));
}

execute(str);

Again, don't use such things even you could. :-)

A better approach, if you really want to have kind of readable thing, would be to have a function like this:

function foo(send) {
  return {
     to: function (country) {
        alert(send + " " + country);
     }
  }
}

foo("Hello").to("USA");

A related project, was to create custom operators in JavaScript. I did it using Esprima. You can check out the result here. There is even an online demo for that.

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • Hang on hehe. What does evil? I mean eval do? – Asperger Oct 04 '15 at 18:50
  • I mean why do we need it for a string replace in this case? – Asperger Oct 04 '15 at 18:50
  • @Asperger There are many posts about why using `eval` is bad. Here is [one of them](http://stackoverflow.com/a/86580/1420197). You need to use `eval` here to execute the code. You have as input `function foo(send TO country) { alert(send + ' ' + country); }`–but this is invalid JS code. You have to replace the ` TO ` into `, `. That's what replace is for. – Ionică Bizău Oct 04 '15 at 18:52
  • This would fail in so many cases, like variables with `TO` in them and other similar stuff, you want to work with the syntax tree. – Benjamin Gruenbaum Oct 04 '15 at 18:56
  • Like in your question I answered here: http://stackoverflow.com/questions/20762338/how-would-i-extend-the-javascript-language-to-support-a-new-operator except instead of adding an operator, you'd add the `TO` token to the parser and replace it with a `,`. – Benjamin Gruenbaum Oct 04 '15 at 18:57
  • @BenjaminGruenbaum Yeah, obviously. That's just a quick example how this could have been done, but it should not. :) – Ionică Bizău Oct 04 '15 at 18:58
  • 1
    Wow you are talented. I like your plugin. They should have integrated it into jquery or something. Damn, pretty good. – Asperger Oct 04 '15 at 19:03
  • @Asperger Thanks! If you think my answer answers your question, don't forget to click the `✔` button on the left side of the answer. Best regards! – Ionică Bizău Oct 04 '15 at 19:05
  • Your answer is perfect. Thanks – Asperger Oct 04 '15 at 19:06
  • 1
    Sorry if this sounds rude, but i shed a tear from lol'ing @ "They should have integrated it into jquery" – wiesion Oct 04 '15 at 19:07
  • 1
    @wiesion I think it should be a web-component. Haha! :D – Ionică Bizău Oct 04 '15 at 19:08
  • Welcome to **sarcasm**overflow :) – wiesion Oct 04 '15 at 19:10
  • Haha looks like you were pretty curious yourself: http://stackoverflow.com/questions/20728460/is-it-possible-to-create-custom-operators-in-javascript – Asperger Oct 04 '15 at 19:15
  • @Asperger [Yes, I am](http://stackoverflow.com/search?q=user%3A1420197+%5Bnan%5D)! `:-)` – Ionică Bizău Oct 05 '15 at 02:18
1

Another way to do this would be to run your code through the C++ preprocessor as part of your build step, and have this at the top of your code:

#define TO ,

Of course, I think this is a really bad idea, and future you in charge of maintenance will hate you for it.

1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239