Jump to content

Does the school cover the "in" operator?


jeffman

Recommended Posts

I just ran across a script using the JavaScript "in" operator. It's related, sorta, to the for . . . in structure. Mozilla has a write-up here.Basically, it returns true/false if an object has a certain property. I thought at first it was the same as doing something like!!object.propertyOne difference is that it returns true if the property exists but is undefined, so it's got a real use, even if it's a small one.I'd never seen it before, and I don't think the School covers it. At least, all my ultra-sophsticated Google techniques can't turn it up at the School.

Link to comment
Share on other sites

That's quite a hawt key word I might have to use it. And I haven't seen it on w3schools but I have to admit there are a lot of hidden sections that you stumble across while browsing the references. I don't think w3school has a keywords reference for JavaScript though it would be really good if they did.

Link to comment
Share on other sites

Actually, I'm only assuming a relationship between in and for . . . in, the same way PHP's each seems to be related to for each. It makes a kind of sense, but I don't really know. So I don't think it was a stupid question, and easy enough to check, as you discovered. EDIT. In fact, Mozilla's site indicates that for ... in was implemented in JavaScript 1.0, and in was implemented in 1.4, so several years apart. Further, this was the 1990s, so a 3-4 year gap was like the age of the universe.EDIT 2. OTOH, Wikipedia has a chart the matches javaScript versions to different browser versions. It looks like Firefox is the only browser that really has kept pace with newer versions of JavaScript, at least till Explorer 9. Is this really correct?EDIT 3. Gosh, it might be. I've been trying out some JavaScript stuff from version 1.6 or higher that I've never used before, and now I know why. It works in Firefox but not in Safari (which is all I'm bothering to cross check at the minute).Examples: for each...in and Iterator . This could be useful stuff. I'm not exactly sure what I've been paying attention to. Probably I just know what works in IE7 and ignore the rest.

Link to comment
Share on other sites

I've used the "in" operator a few times. It works on all the browsers I'm interested in developing for. W3Schools covers for( in ) but not "in" itself.I've only seen one practical use for "in" and it's to copy objects or arrays without taking the prototype properties:

function copyObject(obj) {  var i;  var x = new Object();  for(i in obj) {	if(!(i in Object.prototype)) {	  x[i] = obj[i];	}  }  return x;}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...