473,382 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

The use of the in keyword in a comparision??

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
8 4774
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
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
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
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
>> 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Florian Lindner | last post by:
Hello, I've read the chapter in the Python documentation, but I'm interested in a a more in-depth comparision. Especially regarding how pythonic it is and how well it performs and looks under...
1
by: John Black | last post by:
Hi, In using find_if() you need to name a comparision function which normally is a static function, but sometimes it is really inconvinient for this, let's take this example, class MyClass{...
2
by: Ashwin Kambli | last post by:
Hi, I am doing a study on the performance comparision of C# and Java. Links to any articles on this topic will be greatly appreciated. Thanking you, Ashwin
9
by: jaym1212 | last post by:
Execution of the following simple code results in variable z being assigned the value of 1 ... x = 234; y = 234; z = (x == y); .... but I wanted z to be 234. What is the most efficient...
3
by: kd | last post by:
Hi All, How to perform case-insensitive comparision of strings? Would there be some kind of an indicator, which when set to true, would allow case-insenitive comparision of strings using...
2
by: I Don't Like Spam | last post by:
I know this should be simple but I can't find it. Dim A as new object Dim B as object B = A Do Bunch of stuff Check if B still = A
3
by: krallabandi | last post by:
Hi, I have a requirement to compare 2 ASCII text files and show the results in an aspx form. The comparision algorithnm, out put should be similar to VSS (differences between 2 files). I...
2
by: nirav.lulla | last post by:
I have been given the task to come up with Requirements, Comparision and Migration document from Shadow Direct to DB2 Connect. I am very new much to all this, but kind of know a little bit about...
3
by: abctech | last post by:
I have an Html page, user enters a Date (dd-mm-yyyy) here. There's a servlet connected in the backend for processing this submitted information, it must have a method to compare this entered date...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.