Question - and deeply embarrassed...

Discussion in 'Javascript' started by Mark Preston, Aug 10, 2004.

  1. Mark Preston

    Mark Preston Guest

    I know... I should be able to figure this out. Its silly and its simple
    but its driving me crackers. Don't tell me I should leave it a while and
    come back to it - I know I should and it will probably be blatantly
    obvious when I do. But at the moment its driving me nuts...

    I have a pop-up, pop-down set of layers. They are turned on and off by
    menu links. No problem, except that clicking a link for a layer when the
    layer is already displayed switches it off. I'm trying to prevent that
    with a simple, one-line, conditional:-

    "layer" is the layer to switch on.
    "set_layer" is the currently displayed layer.
    "hideIt()" hides the specified layer.

    My one-liner is:

    if (!layer==set_layer) hideIt(set_layer);

    Now why - and I know I should know this - does this condition never hide
    a layer?
     
    Mark Preston, Aug 10, 2004
    #1
    1. Advertising

  2. On Tue, 10 Aug 2004 16:01:56 +0100, Mark Preston
    <> wrote:

    [snip]

    > "layer" is the layer to switch on.
    > "set_layer" is the currently displayed layer.
    > "hideIt()" hides the specified layer.
    >
    > My one-liner is:
    >
    > if (!layer==set_layer) hideIt(set_layer);
    >
    > Now why - and I know I should know this - does this condition never hide
    > a layer?


    Because of the operator precedence. The negation operator has higher
    precedence then the comparison operator. This means that the object
    reference in 'layer' is logically negated and becomes a boolean. If the
    reference is an object (and it probably is), '!layer' will evaluate to
    false. If 'set_later' is is an object (and that probably is, too), it will
    evaluate to true. True doesn't equal false, so hideIt() is never called
    when it should be.

    What you intended is either

    if(layer != set_layer) ...

    or

    // Force the reference comparison first
    if(!(layer == set_layer)) ...

    Good luck,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail
     
    Michael Winter, Aug 10, 2004
    #2
    1. Advertising

  3. Mark Preston

    Jeff North Guest

    On Tue, 10 Aug 2004 16:01:56 +0100, in comp.lang.javascript Mark
    Preston <> wrote:

    >| I know... I should be able to figure this out. Its silly and its simple
    >| but its driving me crackers. Don't tell me I should leave it a while and
    >| come back to it - I know I should and it will probably be blatantly
    >| obvious when I do. But at the moment its driving me nuts...
    >|
    >| I have a pop-up, pop-down set of layers. They are turned on and off by
    >| menu links. No problem, except that clicking a link for a layer when the
    >| layer is already displayed switches it off. I'm trying to prevent that
    >| with a simple, one-line, conditional:-
    >|
    >| "layer" is the layer to switch on.
    >| "set_layer" is the currently displayed layer.
    >| "hideIt()" hides the specified layer.
    >|
    >| My one-liner is:
    >|
    >| if (!layer==set_layer) hideIt(set_layer);


    You have:
    if( not layer==set_layer) hideIt(set_layer);

    Remove the exclamation symbol from the statement.

    >| Now why - and I know I should know this - does this condition never hide
    >| a layer?


    ---------------------------------------------------------------
    All I can say to the fundamentalist christians is:
    BRING BACK THE LIONS BRING BACK THE LIONS
    BRING BACK THE LIONS BRING BACK THE LIONS
    BRING BACK THE LIONS BRING BACK THE LIONS

    : Remove your pants to reply
    ---------------------------------------------------------------
     
    Jeff North, Aug 10, 2004
    #3
  4. Mark Preston

    Mark Preston Guest

    Michael Winter wrote:

    > On Tue, 10 Aug 2004 16:01:56 +0100, Mark Preston
    > <> wrote:
    >
    > [snip]
    >
    > Because of the operator precedence.[snip]

    Boom! (Sound of shooting own foot)...

    I knew it was simple and I should have realised it. I missed out the
    extra brackets...
     
    Mark Preston, Aug 10, 2004
    #4
  5. Mark Preston

    Mark Preston Guest

    Mark Preston wrote:

    > [snip]
    >
    > My one-liner is:
    >
    > if (!layer==set_layer) hideIt(set_layer);
    >
    > Now why - and I know I should know this - does this condition never hide
    > a layer?


    Thanks for all the help folks - its nice to know there is someone out
    there when you have one of the inevitable "blonde moments" (as we call
    them here in the UK). Wonder why it is we poor coders can spend eight
    hours a day, five days a week doing this stuff and then one day just
    forget how to do one particular line...?
     
    Mark Preston, Aug 11, 2004
    #5
  6. JRS: In article <
    et>, dated Tue, 10 Aug 2004 18:28:51, seen in news:comp.lang.javascript,
    Robert <> posted :
    >I avoid:
    > if( ! (a==b || c==d) ).
    >
    >I find working out the truth tables in my mind hard.


    Just LEARN - not (A or B) = (not A) and (not B)
    not (A and B) = (not A) or (not B)

    See <URL:http://www.merlyn.demon.co.uk/js-logic.htm>.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Aug 11, 2004
    #6
  7. Mark Preston

    Jeff North Guest

    On Wed, 11 Aug 2004 11:31:21 +0100, in comp.lang.javascript Mark
    Preston <> wrote:

    >| Mark Preston wrote:
    >|
    >| > [snip]
    >| >
    >| > My one-liner is:
    >| >
    >| > if (!layer==set_layer) hideIt(set_layer);
    >| >
    >| > Now why - and I know I should know this - does this condition never hide
    >| > a layer?
    >|
    >| Thanks for all the help folks - its nice to know there is someone out
    >| there when you have one of the inevitable "blonde moments" (as we call
    >| them here in the UK).


    Hey!!! I resemble that remark :)
    Yes I *am* blonde :)

    >| Wonder why it is we poor coders can spend eight
    >| hours a day, five days a week doing this stuff and then one day just
    >| forget how to do one particular line...?


    ---------------------------------------------------------------
    : Remove your pants to reply
    ---------------------------------------------------------------
     
    Jeff North, Aug 11, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward C. Jones

    Pickle vs. eval for deeply nested objects

    Edward C. Jones, Feb 18, 2004, in forum: Python
    Replies:
    0
    Views:
    488
    Edward C. Jones
    Feb 18, 2004
  2. Ori Y
    Replies:
    1
    Views:
    404
    Bob Ippolito
    Feb 28, 2004
  3. Kirk Strauser
    Replies:
    1
    Views:
    302
    Kirk Strauser
    Jun 11, 2004
  4. Gerald Britton
    Replies:
    3
    Views:
    253
    Steven D'Aprano
    Jan 30, 2011
  5. Stephen Hansen
    Replies:
    4
    Views:
    257
    Stephen Hansen
    Jan 31, 2011
Loading...

Share This Page