admin管理员组

文章数量:1323729

Is there a way through some sort of meta or pre-processor to tell JavaScript that the word AND equates to && and the word OR equates to || and <> equates to !===?

Maybe equate THEN to {

END to }

NOT to !

Is there a way through some sort of meta or pre-processor to tell JavaScript that the word AND equates to && and the word OR equates to || and <> equates to !===?

Maybe equate THEN to {

END to }

NOT to !

Share Improve this question edited May 22, 2011 at 2:00 Phillip Senn asked May 22, 2011 at 1:48 Phillip SennPhillip Senn 47.7k91 gold badges261 silver badges378 bronze badges 1
  • Are you talking about actual code written with infix notation? Like you have "if (a and b) { }" in code? – kristopolous Commented May 22, 2011 at 1:52
Add a ment  | 

4 Answers 4

Reset to default 6

No. Don't do it.

... well, there is a way, but don't do it anyways...

Here it is....

function loadScriptWithReplacements(url) {
     try {
          var rq = null;
          if(window.XMLHttpRequest)
               rq = new XMLHttpRequest();
          else if(window.ActiveXObject) {
               try {
                    rq = new ActiveXObject("Msxml2.XMLHTTP");
               } catch(o) {
                    rq = new ActiveXObject("Microsoft.XMLHTTP");
               }
          }
          rq.open("GET", url, false);
          rq.send(null);
          document.write('<script>' + rq.responseText.replace(/(and|or|\<\>)/g, function(r) {
               return {
                    'and': '&&',
                    'or' : '||',
                    '<>' : '!=='
               }[r];
          }) + '</script>');
     } catch(ex) {
          // We can't have a fallback because you're using invalid JavaScript :(
          throw new Error("Could not use Ajax.");
     }
}

But, DO NOT DO THAT! You could also perform the replacements server-side, but DON'T DO THAT EITHER!

Doing it server-side increases the strain on your server, doing something that could be avoided by just remembering how to write JavaScript correctly.

Coffescript uses a rewriter to provide syntactic sugar. http://jashkenas.github./coffee-script/

From the section titled "If, Else, Unless, and Conditional Assignment" The coffeescript

if happy and knowsIt
  clapsHands()
  chaChaCha()
else
  showIt()

is rewritten to

if (happy && knowsIt) {
  clapsHands();
  chaChaCha();
} else {
  showIt();
}

No, there is no way to do that.

No, there is no way to do that. Well, theoretically, it could be done, but it would be slow and or difficult. Is there a good reason to use AND in place of && and OR in place of || ?

I suppose you could pre-process your code using awk,sed,perl,python,bash,[insert favrotie scripting language], but why?

本文标签: javascriptampamp means AND means ORStack Overflow