Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > The use of the in keyword in a comparision??

Reply
Thread Tools

The use of the in keyword in a comparision??

 
 
Morgan
Guest
Posts: n/a
 
      12-12-2005
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");

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-12-2005
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
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      12-12-2005
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/docs...rs: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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-13-2005
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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-13-2005
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
 
Reply With Quote
 
Morgan
Guest
Posts: n/a
 
      12-13-2005
>> 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

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-13-2005
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
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      12-13-2005
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/
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      12-14-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: keyword checker - keyword.kwlist Hamilton, William Python 4 05-13-2007 06:31 AM
keyword checker - keyword.kwlist tom@finland.com Python 6 05-10-2007 04:53 PM
use weird char or symbol for quicksearch keyword Splibbilla Firefox 0 01-30-2007 12:50 AM
What means? How to use : transient keyword! Se-Jin Park Java 1 11-18-2004 06:32 AM
what does the native keyword do actually ? and how does it work ?when we can use it ? Alek Nazarian Java 7 10-22-2003 04:33 PM



Advertisments