![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||
| |||
|
|
i=++i; is equivalent to the two lines: i++; i=i; So, yes, it works, but it's silly to do it that way. |
#12
| |||
| |||
|
|
On Dec 16, 2:03 pm, Doug Miller wrote: Steve wrote: Several textbooks, including Javascript for Dummies (2005), show the "i=++i" method of incrementing a variable. You sure about that? Or did you mean i = i+1 ? Yes I'm sure, it is in the table on page 50 of the Dummies book for example, except they use x instead of i. And it works too, try it! snip |
#13
| |||
| |||
|
|
On Sun, 16 Dec 2007 02:46:22 -0800, Steve wrote: Several textbooks, including Javascript for Dummies (2005), show the "i=++i" method of incrementing a variable. I have been using this method but I have been recently informed by some experts in the Google Maps API group that this method is confusing and wasteful of resources and is now redundant and I should use "++i" or "i=i+1". Any strong opinions in this group? Profile your code first! There is absolutely zero point in optimizing something which may have 1% or less performance degradation on your code. Instead, work at optimizing your code which consumes most of the CPU. Use firebug's profiler to determine the functions your code is spending most of its time. |
#14
| |||
| |||
|
|
Steve wrote: On Dec 16, 2:03 pm, Doug Miller wrote: Steve wrote: Several textbooks, including Javascript for Dummies (2005), show the "i=++i" method of incrementing a variable. You sure about that? Or did you mean i = i+1 ? Yes I'm sure, it is in the table on page 50 of the Dummies book for example, except they use x instead of i. And it works too, try it! snip The 'it works' criteria only gets you so far. The outcome of - i = ++ i; - is not different from - ++i; -, but the second operation in the expression mans that the first will take (very fractionally) longer and if the outcomes are the same that second operation must be redundant. Remember that books on javascript are more likely to be written by journalists than programmers, and even if written by programmers they are unlikely to be good javascript programmers. David Flanagan's "javascript: The definitive Guide" is, by a very wide margin, the best (or, more normally, the least bad) book available on javascript, but it is quite obvious from its text that even its author has little practical experience of browser scripting (certainly in comparison to his experience of writing books) and in many cases is just parroting the assertions of other, less then well informed, sources. Richard. |
#15
| |||
| |||
|
|
On Dec 16, 2:03 pm, spamb... (AT) milmac (DOT) com (Doug Miller) wrote: In article 5ecc086d-5d4c-4350-9230-cc04bbcfd... (AT) b1g2000pra (DOT) googlegroups.com>, Steve stephen.jo... (AT) googlemail (DOT) com> wrote: Several textbooks, including Javascript for Dummies (2005), show the "i=++i" method of incrementing a variable. You sure about that? Or did you mean i = i+1 ? Yes I'm sure, it is in the table on page 50 of the Dummies book for example, except they use x instead of i. And it works too, try it! |
#16
| |||
| |||
|
|
So where did the good javascript programmers learn to program javascript? |
#17
| |||
| |||
|
|
i=i+1; i+=1; i++; ++i; |
#18
| |||
| |||
|
|
but if f is some function, f(++i) means something different then f(i++) |
|
i+=1; and i=i+1; generate the same code. (or at least should) |
|
i++ is incremented after it comes off the stack. ++i is incremented before it goes on the stack. |
![]() |
| Thread Tools | |
| Display Modes | |
| |