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: Luke Matuszewski <matuszewski.lukasz@gmail.com>
Date: Thu Feb 09 2006 - 17:31:39 CET

Luke Matuszewski napisal(a):
> if(document.write) {
> if(document["write"]) {
> var prop = [...];
>
> if(document[prop]) {

, which will not work if document has a write property and it has null
or undefinded value (so in operator can tell if object has property in
question even if this property is null or undefined).

> if("write" in document) {

, results true if document has write property even if it is null or
undefined.

I do not see other reasons for using 'in' operator (detecting property
of object even if it is null or undefined). Here is a test case:

var ob = { prop: " [ property value ] " };
if(ob.prop) {
  alert("feature detection: " + ob.prop);
}

if("prop" in ob) {
  alert("in operator: " + ob.prop);
}

ob.prop = null;

if(ob.prop) {
  alert("feature detection(ob.prop==null): " + ob.prop);
}

if("prop" in ob) {
  alert("in operator(ob.prop==null): " + ob.prop);
}

ob.prop = window.window1234567890; /* ob.prop == undefined */

if(ob.prop) {
  alert("feature detection(ob.prop==null): " + ob.prop);
}

if("prop" in ob) {
  alert("in operator(ob.prop==null): " + ob.prop);
}

B.R.
Luke Matuszewski
Received on Mon May 1 03:12:19 2006