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
4 Answers
Reset to default 6No. 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
版权声明:本文标题:javascript - && means: AND, || means OR - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742121739a2421741.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论