View Full Version : Quick logic question

h8ids

01-20-2006, 03:07 PM

Examining an online tut. Scenarios 1 & 2 make sense. But why wouldn't scenario 3 have a Y value of 3?

Does the = Operator force the Y to take the initial value prior to incrementing?

Using x=2 for each example below:

1) x++

Result: x=3

2) y= ++x

Result: 3

3) y= x++

Result: y=2

quite simple, it is matter of order of operations (++x and x++ are not quite the same, from this point of view)

y= x++

y will be assigned the value of x, then x++ is incremented

y=++x

now first x is incremented, then y is assigned the value of incremented x

Does the = Operator force the Y to take the initial value prior to incrementing?

In fact ++ operator force that if placed in the right side of the increment.

You could have noticed that if checking for the value of x as well

x=2;

y=x++

alert(y+'|'+x) will return 2|3

Is useful in case you need to use both values of the incremented variable (before and after increment operation), for some reson, inisde the same turn of a loop

h8ids

01-20-2006, 03:56 PM

Great to know. Thanks.

Here's one last question.

The tut states that field1 would be equal to "document.form1.field1". I understand the concept of with caching "document.form1". But how does JS derive the solution "document.form1.field1"?

What happens to "test"?

Wouldn't the solution be "field1.document.form1"?

with (document.form1){

field1.value="test"

}

quite simple, it is matter of order of operations (++x and x++ are not quite the same, from this point of view)

y= x++

y will be assigned the value of x, then x++ is incremented

y=++x

now first x is incremented, then y is assigned the value of incremented x

In fact ++ operator force that if placed in the right side of the increment.

You could have noticed that if checking for the value of x as well

x=2;

y=x++

alert(y+'|'+x) will return 2|3

Is useful in case you need to use both values of the incremented variable (before and after increment operation), for some reson, inisde the same turn of a loop

Wouldn't the solution be "field1.document.form1"?

with (document.form1){

field1.value="test"

}

Nope.

with (document.form1){

field1.value="test"

}

is equivalent with

document.form1.field1.value="test"

It is useful if you have a lot of elements to handle and you vant to avoid writing the full reference each time

Alternatively, you may use

var f= document.form1;

f.field1.value="test"

h8ids

01-20-2006, 04:15 PM

Thanks again.

The result still looks weird to me, but I can live with it.

Cheers.

Nope.

with (document.form1){

field1.value="test"

}

is equivalent with

document.form1.field1.value="test"

It is useful if you have a lot of elements to handle and you vant to avoid writing the full reference each time

Alternatively, you may use

var f= document.form1;

f.field1.value="test"

Why weird?

with(something){}

simply will force any object which are to be set inside the control structure to get the "root" something

For instance, another example, instead of using:

x = Math.round( Math.LN2 + Math.E + Math.pow( y, 4 ));

you may simplify as:

with(Math){

x=round(LN2+E+pow(y,4));

}

liorean

01-20-2006, 04:43 PM

Also note, however, that the with statement has considerably bad performance. In the general case, you only want to use it if every identifier in the statement is a member of the same object. In general the best choice is caching the object in a variable and using it just for those cases it's needed.

There are other reasons you might want to use it, related to how it changes scope resolutions etc. when creating object oriented JavaScripts, but that's another story entirely.

h8ids

01-20-2006, 05:58 PM

Aaahhhh, I see. Presenting it in a "Full circle" example makes it easy to understand.

Why weird?

with(something){}

simply will force any object which are to be set inside the control structure to get the "root" something

For instance, another example, instead of using:

x = Math.round( Math.LN2 + Math.E + Math.pow( y, 4 ));

you may simplify as:

with(Math){

x=round(LN2+E+pow(y,4));

}

h8ids

01-20-2006, 06:00 PM

Thanks for the cautionary note.

I won't try any acrobatic stunts. :thumbsup:

Also note, however, that the with statement has considerably bad performance. In the general case, you only want to use it if every identifier in the statement is a member of the same object. In general the best choice is caching the object in a variable and using it just for those cases it's needed.

There are other reasons you might want to use it, related to how it changes scope resolutions etc. when creating object oriented JavaScripts, but that's another story entirely.

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.