views:

146

answers:

2

Hi Folks,

Can someone tell me since which ECMA version the IN operator is available and which browsers (versions) support it ?

Explanation:

The IN-operator can be used like the following:

var myObject = {
    Firstname: 'Foo',
    Lastname: 'Bar'
};

if('Lastname' in myObject){
    // Lastname is an attribute of myObject
}
A: 

According to MDC, it's implemented in JavaScript 1.4.

According to Wikipedia:

  • Netscape Navigator 6.0
  • Firefox 1.0+
  • IE 5.5+
  • Opera 6.0+
  • Safari 3.0+
  • Chrome 1.0+

So I think you're probably OK :)

Skilldrick
because I can do `undefined = 'defined!';` earlier in the code, which is *obviously* beneficial.
Eric
@jAndy, ok, I've edited that out.
Skilldrick
Really, you should have put that as another answer, as now our comments make no sense.
Eric
+5  A: 

It is defined in ECMAScript 3rd edition. It is available in IE5.5-IE8 (JScript version 1), Firefox 1+, Chrome (all versions), Opera, Safari and probably most other javascript supporting browsers.

You can use it safe in the knowledge that it will work.

You should err on the side of caution when using it to check event support. All implementations except Mozilla support "eventname" in element as a test for DOM events, Firefox will result in false here unless a handler is defined.

"onclick" in document.body; // -> false in Fx, true in others
document.body.setAttribute("onclick", "");
typeof(document.body.onclick == "function") // -> true in Fx
Andy E
It's only in IE 5.5+; IE 5.0 was the ‘problem browser’ for `in`, though obviously that worry is long gone now.
bobince
FWIW I couldn't see it in the 2nd ed @ http://www.ecma-international.org/publications/standards/Ecma-262-arch.htm
Alex K.
@bobince: fixed. @Nick Craver, isn't [ECMAScript 5th edition](http://en.wikipedia.org/wiki/ECMAScript#ECMAScript.2C_5th_Edition) ECMAScript 3.1?
Andy E
@Nick: it certainly is in 3.0, even though IE didn't support it at the time of publication. ECMAScript 3.1 was the development name for what became the Fifth Edition.
bobince
@bobince - You're indeed right, I had the *2nd* edition, not the 3rd up when comparing and couldn't find section 11.8.7, it is in 3.0.
Nick Craver
@bobince: When I put IE5, I was going by the MSDN documentation for the [in operator](http://msdn.microsoft.com/en-us/library/9k25hbz2(v=VS.85).aspx) which states that it was in JScript version 1, which was implemented in IE 3.0. I put 5 as a safer bet because IE3 isn't widely used. I assume the docs are wrong? They have been in the past *once or twice* :-)
Andy E
@Andy: you're right, that doc is indeed nonsense! I've got IE5/Win98 here and `in` definitely doesn't work.
bobince
@bobince: thought as much, not the first time I've been misled by MSDN :-)
Andy E