The use of the in keyword in a comparision??

Discussion in 'Javascript' started by Morgan, Dec 12, 2005.

  1. Morgan

    Morgan Guest

    Hi all,

    I was messing around with someone elses script when I noticed that he
    had used the in keyword in an if conditional to compare a string with a
    object. I've never seen this used and was wondering about the logic
    behind it. I had previously assumed that the in keyword passed the
    properties of an object to a variable, as you do in a for/in loop. How
    is it that it can be used in a comparsion?

    example:

    var elements = {'html':true}

    var myElement = "html";

    if(myElement in elements)
    alert("True");
    Morgan, Dec 12, 2005
    #1
    1. Advertising

  2. Morgan wrote:

    > I was messing around with someone elses script when I noticed that
    > he had used the in keyword in an if conditional to compare a string
    > with a object.


    The `in' operation in a boolean expression does not compare a string
    with an object, it evaluates to `true' iff the referred object has a
    property with the string as name or inherits that from its prototype.

    RTFM: <URL:http://jibbering.com/faq/#FAQ3_2>


    PointedEars
    Thomas 'PointedEars' Lahn, Dec 12, 2005
    #2
    1. Advertising

  3. Morgan

    RobG Guest

    Thomas 'PointedEars' Lahn wrote:
    > Morgan wrote:
    >
    >
    >>I was messing around with someone elses script when I noticed that
    >>he had used the in keyword in an if conditional to compare a string
    >>with a object.

    >
    >
    > The `in' operation in a boolean expression does not compare a string
    > with an object, it evaluates to `true' iff the referred object has a
    > property with the string as name or inherits that from its prototype.
    >
    > RTFM: <URL:http://jibbering.com/faq/#FAQ3_2>
    >


    Gee Thomas, that was a bit obscure - Section 3.2 has 30 links. :p

    For the OP, it's covered in the ECMAScript Language Specification
    Section 11.8.7 and Mozilla Developer Center here:

    <URL:
    http://developer.mozilla.org/en/doc...rence:Operators:Special_Operators:in_Operator
    >


    Another way of writing it is:

    var elements = {'html':true}
    var myElement = "html";
    if( elements[myElement] ) {
    alert("True");
    }


    Presumably the idea is to use the value if the property exists, so using
    a style that even Thomas should be happy with:

    var o = elements && elements[myElement];
    // if elements exists and has a property with a name that
    // matches the value of myElement, 'o' is set to the
    // value of elements[myElement]
    // Otherwise 'o' will be set to undefined.

    if ('undefined' == typeof o){ // Or test for ! a specific type

    // Handle not getting (a specific typeof) o.
    alert('No property with name ' + myElement);
    return;
    }

    // Use o...
    alert( o );



    --
    Rob
    RobG, Dec 12, 2005
    #3
  4. RobG wrote:

    > Thomas 'PointedEars' Lahn wrote:
    >> Morgan wrote:
    >>> I was messing around with someone elses script when I noticed that
    >>> he had used the in keyword in an if conditional to compare a string
    >>> with a object.

    >> The `in' operation in a boolean expression does not compare a string
    >> with an object, it evaluates to `true' iff the referred object has a
    >> property with the string as name or inherits that from its prototype.
    >>
    >> RTFM: <URL:http://jibbering.com/faq/#FAQ3_2>

    >
    > Gee Thomas, that was a bit obscure - Section 3.2 has 30 links. :p
    > [...]


    I explained the operator _and_ referred the OP to the reference materials
    that he obviously had never considered to read before posting. "messing
    around with someone elses script" and "compare a string with a object"
    clearly indicates that it is a Good Idea to do that now.

    > For the OP, it's covered in the ECMAScript Language Specification


    Therefore the link to the list of all online resources.

    > Section 11.8.7 and Mozilla Developer Center here:


    JavaScript is not the only ECMAScript implementation discussed here.
    Therefore ...


    HTH

    PointedEars
    Thomas 'PointedEars' Lahn, Dec 13, 2005
    #4
  5. RobG wrote:

    > [...]
    > Another way of writing it is:
    >
    > var elements = {'html':true}
    > var myElement = "html";
    > if( elements[myElement] ) {
    > alert("True");
    > }


    That is _not_ equivalent to the `in' operation. That latter boolean
    expression will only evaluate to `true' as long as there is a property
    of that name _and_ the value of that property is a true-value.

    var
    elements = {html: null},
    myElement = "html";

    if (elements[myElement])
    {
    // this will never be called although the property exists
    alert("True");
    }

    > Presumably the idea is to use the value if the property exists, so using
    > a style that even Thomas should be happy with:


    I am not. And I am not unhappy with it. It is simply not equivalent.

    > var o = elements && elements[myElement];
    > // if elements exists and has a property with a name that
    > // matches the value of myElement, 'o' is set to the
    > // value of elements[myElement]
    > // Otherwise 'o' will be set to undefined.


    This will break with a ReferenceError if `elements' is not
    variable-instantiated before, i.e. if `elements' does not exist.

    > if ('undefined' == typeof o){ // Or test for ! a specific type
    > // Handle not getting (a specific typeof) o.
    > alert('No property with name ' + myElement);
    > return;
    > }


    As I pointedEars^H^H^H^H out ;-) before, comparing the value of the
    `typeof` operation against the string value "undefined" also is not
    equivalent to the `in' operation [I do not think there is an equivalent
    -- hasOwnProperty() returns `true' only if the object itself has the
    property]. It merely serves as a viable alternative for AOM/DOM
    feature testing. Try this:

    var o = {foo: undefined};
    alert(typeof foo.bar); // "undefined"

    That is probably a theoretical construct, however, if the `foo' property
    would be assigned a variable (not fixed) value, entirely possible (ignoring
    that implementations may allow to redefine the global `undefined' property
    in which case it would be already a variable value.)


    PointedEars
    Thomas 'PointedEars' Lahn, Dec 13, 2005
    #5
  6. Morgan

    Morgan Guest

    >> Another way of writing it is:

    >> var elements = {'html':true}
    >> var myElement = "html";
    >> if( elements[myElement] ) {
    >> alert("True");
    >> }


    >That is _not_ equivalent to the `in' operation.


    No luck there then, Rob. ;-) Imagine getting this guy a christmas gift.

    >I explained the operator _and_ referred the OP to the reference materials
    >that he obviously had never considered to read before posting.


    I did make an effort to look for a defination of the in keyword,
    however I couldn't find topics on it in the groups history or on google
    search, lots of web pages appear to match the word "in". Also your faq
    is does not have a list of keyword definations, considering the amount
    of quizzes about the "this" keyword generally, this might be a good
    idea. Whats more I do have an ECMAScript spec on my desktop, and this
    is what I found under in operator:

    11.8.7 The in operator
    The production RelationalExpression : RelationalExpression in
    ShiftExpression is evaluated as follows:
    1. Evaluate RelationalExpression.
    2. Call GetValue(Result(1)).
    3. Evaluate ShiftExpression.
    4. Call GetValue(Result(3)).
    5. If Result(4) is not an object, throw a TypeError exception.
    6. Call ToString(Result(2)).
    7. Call the [[HasProperty]] method of Result(4) with parameter
    Result(6).
    8. Return Result(7).

    return WTF???;

    Forgive me, if I didn't have a burning fascination to find to crawl
    through 188 pages worth at the time. Frankly it was just something that
    I was mildly interested in. I really didn't think it would be such a
    big deal for you.

    Here is a new keyword for you: pedantic
    Morgan, Dec 13, 2005
    #6
  7. Morgan wrote:

    > 11.8.7 The in operator
    > The production RelationalExpression : RelationalExpression in
    > ShiftExpression is evaluated as follows:
    > [...]
    > return WTF???;


    I already told (you) what it evaluates to. If you are not able
    to understand the abstract specification, you are free to try the
    Client-Side JavaScript Reference at developer.mozilla.org or
    the MSDN Library at msdn.microsoft.com/library. Both are included
    in the FAQ section I pointed to.

    > [...]
    > Here is a new keyword for you: pedantic


    Here is a new keyword for you: ignorant.


    score adjusted

    PointedEars
    Thomas 'PointedEars' Lahn, Dec 13, 2005
    #7
  8. Morgan

    Randy Webb Guest

    Thomas 'PointedEars' Lahn said the following on 12/13/2005 4:18 PM:
    > Morgan wrote:


    <snip>

    >>[...]
    >>Here is a new keyword for you: pedantic

    >
    >
    > Here is a new keyword for you: ignorant.


    Too bad they both apply to you Thomas.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Dec 13, 2005
    #8
  9. Morgan

    RobG Guest

    Morgan wrote:
    >>>Another way of writing it is:

    >
    >
    >>> var elements = {'html':true}
    >>> var myElement = "html";
    >>> if( elements[myElement] ) {
    >>> alert("True");
    >>> }

    >
    >
    >>That is _not_ equivalent to the `in' operation.

    >
    >
    > No luck there then, Rob. ;-) Imagine getting this guy a christmas gift.


    I'm OK with Thomas... particularly since I baited him anyway.


    > I did make an effort to look for a defination of the in keyword,
    > however I couldn't find topics on it in the groups history or on google
    > search, lots of web pages appear to match the word "in".


    A bit like searching for keywords like 'and' or 'not' I imagine! :)


    [...]

    > Forgive me, if I didn't have a burning fascination to find to crawl
    > through 188 pages worth at the time.


    In order to find it you would either have to have done that or known
    that you were looking for a relational operator, in which case you would
    already have known the answer. It's a perfectly reasonable question to
    ask here.



    --
    Rob
    RobG, Dec 14, 2005
    #9
    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. john

    Comparision

    john, Mar 17, 2005, in forum: VHDL
    Replies:
    2
    Views:
    471
    Mike Treseler
    Mar 17, 2005
  2. Viktor Popov

    stored procedure result comparision

    Viktor Popov, Jul 20, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    552
    Viktor Popov
    Jul 20, 2004
  3. John Black
    Replies:
    1
    Views:
    387
    Daniel T.
    May 31, 2004
  4. Replies:
    6
    Views:
    448
    Peter Otten
    May 10, 2007
  5. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    349
Loading...

Share This Page