By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,567 Members | 1,670 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,567 IT Pros & Developers. It's quick & easy.

The use of the in keyword in a comparision??

P: n/a
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");

Dec 12 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
Dec 12 '05 #2

P: n/a
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
Dec 13 '05 #3

P: n/a
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
Dec 13 '05 #4

P: n/a
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
Dec 13 '05 #5

P: n/a
>> 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

Dec 13 '05 #6

P: n/a
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
Dec 13 '05 #7

P: n/a
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/
Dec 13 '05 #8

P: n/a
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
Dec 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.