Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    Last edited by Kor; 01-20-2006 at 03:34 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

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

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

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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"
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Regular Coder
    Join Date
    Jan 2006
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks again.
    The result still looks weird to me, but I can live with it.

    Cheers.

    Quote 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"

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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));
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Regular Coder
    Join Date
    Jan 2006
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aaahhhh, I see. Presenting it in a "Full circle" example makes it easy to understand.

    Quote 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));
    }

  • #9
    Regular Coder
    Join Date
    Jan 2006
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the cautionary note.
    I won't try any acrobatic stunts.

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