非典型用于Javascript的++和 - 运算符 [英] Atypical uses for Javascript's ++ and -- operators

查看:90
本文介绍了非典型用于Javascript的++和 - 运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我从Crockford的Javascript:The Good Parts中正确回忆起来,他赞成使用++或 - 运算符,但我也倾向于回忆他不提供对他们来说是一个特别强烈的论据。

If I recall correctly from Crockford's "Javascript: The Good Parts", he is not in favor of using the ++ or -- operators, but I also tend to recall he doesn't provide an especially strong argument against them.

下面是对这些运算符的使用,我发现这些运算符有助于保持我的代码尽可能简洁,特别是在处理函数/方法时当0是第一个可能的有效返回值(以及正整数)时返回-1。我会对++和/或其他非典型用法感兴趣,这些用法有利于在有意义的情况下使用这些运算符。

Below is a use of these operators that I've found helpful in keeping my code as concise as possible particularly when dealing with functions/methods that return -1 when 0 is the first possible valid return value (along with positive integers). I'll be interested in other atypical uses of ++ and/or -- that make a strong argument in favor of using these operators when they make sense.

认为这是为什么要避免在JavaScript中增加(++)和减少( - )运算符?而是它的推论:何时到避免使用它们,而是将它们用于您的优势。当然我可能会弄错,可能有一些原因我不会想到为什么以下是充满危险的,尽管我觉得自己很优雅 - 如果我错过了关于以下内容的次优,我我也想知道这一点

I do not consider this a duplicate of Why avoid increment ("++") and decrement ("--") operators in JavaScript? but rather it's corollary: when to not avoid them, but rather use them to your advantage. Of course I could be mistaken and there could be some reason I'm not thinking of as to why the following is fraught with danger despite seeming to me to be elegant -- if I'm missing something sub-optimal about the following, I'd like to know that too

var substrIdx = str.indexOf(substr);
if (++substrIdx) {
  doSomething(--substrIdx);
}


推荐答案


如果我遗漏了以下内容的次优,我也想知道那个

If I'm missing something sub-optimal about the following, I'd like to know that too

var substrIdx = str.indexOf(substr);
if (++substrIdx) {
  doSomething(--substrIdx);
}


这是次优的。它让人感到困惑,需要通过浏览器传输更多代码,并且需要解释器在运行时做更多的工作。

It's sub-optimal. It is confusing to humans, requires shipping more code over the browser, and requires the interpreter to do more work at runtime.

有人想弄清楚它是什么,有在代码中的不同点推断 substrIdx 的状态 - 它遵守 indexOf 的合约,但它有一个大于 indexOf 的合约,而在 if 之后,它与...没有明确的关系 substr 。。

Someone trying to figure out what it is, has to reason about the state of substrIdx at different points in the code -- "it obeys the contract of indexOf here, but there it's one greater than the contract of indexOf, and after the if it doesn't have a clear relationship to substr.".

它还长于

var substrIdx = str.indexOf(substr);
if (substrIdx>=0) {
  doSomething(substrIdx);
}

缩小器更难缩小,因为 substrIdx 不再是单个作业 var
一个好的minifier会把后者变成像

is harder for minifiers to minify because substrIdx is no longer a single assignment var. A good minifier will turn the latter into something like

var substrIdx = str.indexOf(substr);
substrIdx<0||doSomething(substrIdx);

但是通过不必要的分配可以减少工作量。他们在关闭编译器下进行了比较:

but fewer will do the job with the unnecessary assignment. They compare pretty well under closure compiler:

var a=str.indexOf(substr);++a&&doSomething(--a)

vs

var a=str.indexOf(substr);a>=0&&doSomething(a)

因为闭包编译器没有机会转 a> = 0&& 进入 a< 0 || 但增量和减量仍然更长。

because closure compiler misses the opportunity to turn a>=0&& into a<0|| but the increment and decrement is still longer.

这篇关于非典型用于Javascript的++和 - 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆