Re: 'in' operator and feature detection technique...
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: 'in' operator and feature detection technique...

From: Martin Honnen <mahotrash@yahoo.de>
Date: Thu Feb 09 2006 - 17:45:32 CET

Luke Matuszewski wrote:

> if("write" in document) {

> So i have questioned myself what for ? I can always do:
>
> if(document["write"]) {

> I can only guess that 'in' was
> provided in W. IE 5.5 only for convenience...

The in operator checks whether there is a property with the name as the
first operand, that is much different to converting the property value
to a boolean (which if () does).

Example

var testObject = {
   b : false,
   s : '',
   n: 0
};

var results = '';

for (var propertyName in testObject) {
   results += 'Boolean(obj["' + propertyName + '"]): ' +
Boolean(testObject[propertyName]) + '\r\n';
   results += '"' + propertyName + '" in obj: ' +
(propertyName in testObject) + '\r\n\r\n';
}

results

yields

Boolean(obj["b"]): false
"b" in obj: true

Boolean(obj["s"]): false
"s" in obj: true

Boolean(obj["n"]): false
"n" in obj: true

-- 
	Martin Honnen
	http://JavaScript.FAQTs.com/
Received on Mon May 1 03:12:22 2006