Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. Quick logic question

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++

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

• 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"
}

Originally Posted by Kor
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++

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"

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

Cheers.

Originally Posted by Kor
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));
}

• 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.

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

Originally Posted by Kor
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));
}

• Thanks for the cautionary note.
I won't try any acrobatic stunts.

Originally Posted by liorean
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.

•

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•