views:

284

answers:

5
var a = [1,4,5];
var e = a.length--;

Here e variable will contain 5. But if I do:

var e = a.length-=1;

Here e will contain 2 the number of elements array.
So the first is a language 'tip' to simulate a pop() array methods?

In the language syntax doing:

a--

or

a-=1

is semantically the same.

+2  A: 

This is a basic misconception: If you evaluate a++, a will be evaluated and then incremented while ++a or a+=1 will be incremented first, then evaluated.

In most cases it's better to write these kinds of statements in a more verbose manner because they can quickly lead to confusion about what is going on while not gaining anything (most compilers / interpreters will optimize this anyway).

Jan Jungnickel
+5  A: 
var a = [1, 4, 5];
var e = a.length--;

is the same as

var a = [1, 4, 5];
var e = a.length;
a.length = a.length - 1;

whereas

 var a = [1, 4, 5];
 var e = a.length -= 1;

is the same as

var a = [1, 4, 5];
a.length = a.length - 1;
var e = a.length;

and

var a = [1, 4, 5];
var e = --a.length;

In other words: x-- returns the value before decreasing whereas --x (and x -= 1) returns the value after decreasing. All these code snippets pop the last element off the array a.

Note that in the first example, e = a.length--, e will have the value 3, not 5, therefore it is not the same as a.pop().

Ferdinand Beyer
+1  A: 

That's not what I get.

Rather, I get the expected result. e = 3 in the first instance, and e = 2 in the second.

In the first case, a.length is initially 3 (because javascript arrays are sparse). using the post-decrement, this value (3) is first assigned to e, then a.length is decremented (funny this is possible, actually).

In the second case, 1 is subtracted from 3 to make 2, which is then assigned to e.

So I don't think there's anything fancy going on, really.

Tor Haugen
+1  A: 
var a = [1,4,5];
var e = a.length--;

e will not contain 5. It will contain 3.

The -- postix decrement operator will make no difference here. a.length is 3. The second statement translates as:

  • retrieve array length (result: 3)
  • store result in variable named "e"
  • decrement array length (this step has no effect on the overall outcome for this standalone sample)

Whereas:

var a = [1,4,5];
var e = a.length-=1;

here e will contain 2.

The second statement translates as

  • retrieve array length (result: 3)
  • decrement array length (result: 2)
  • store result in variable named "e"

In any case, you are messing with the array.length property, which you shouldn't do. It causes the kind of nasty bug that is hard to track down because it is result of a side effect you probably did not intend or think of.

Tomalak
A: 

oh yes I read this from the book : Object Oriented Javascript at page 112

The push() method appends a new element at the end of the array. The pop() method removes the last element. a.push('new') works just like a[a.length] = 'new' and a.pop() is the same as a.length--.

and without try the code I post the question!!!

Sorry. So I think this is a book error because pop() returns the last element where a.length-- shrink the array but not returning the element but the array lenght -1

xdevel2000