Reputation: 47605
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 !
Upvotes: 1
Views: 1597
Reputation: 224913
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.
Upvotes: 6
Reputation: 19645
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?
Upvotes: 2
Reputation: 120516
Coffescript uses a rewriter to provide syntactic sugar. http://jashkenas.github.com/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();
}
Upvotes: 3