Object.prototype.hasOwnProperty()

The ECMAScript-262 revision 3 specification includes a method named hasOwnProperty which must be present on all objects. From the spec:

Object.prototype.hasOwnProperty (V)

When the hasOwnProperty method is called with argument V, the following steps are taken:

  1. Let O be this object.
  2. Call ToString(V).
  3. If O doesn't have a property with the name given by Result(2), return false.
  4. Return true.
- (ECMA-262, 3rd edition, page 85, section 15.2.4.5)

Safari (up to and including version 2.0) does not support this method.

Simple Testcase

Code
var theObj = { foo : 'yum' };
if ( theObj.hasOwnProperty )
{
  document.write( "Has 'foo': " + theObj.hasOwnProperty( 'foo' ) + '\n' );
  document.write( "Has 'bar': " + theObj.hasOwnProperty( 'bar' ) + '\n' );
}
else
{
  document.write( "ERROR: ECMAScript hasOwnOwnProperty() method missing!" );
}
Expected Result:
Has 'foo': true
Has 'bar': false
Result in your browser:

'Real World' Testcase

Code
//Simplistic version for illustration purposes only
Object.prototype.toSourceCode = function( )
{
  var theSourceCode = '{\n';
  for ( var thePropName in this )
  {
    if ( !this.hasOwnProperty || this.hasOwnProperty( thePropName ) )
    {
      theSourceCode += '  ' + thePropName + ' : ' + this[ thePropName ] + ',\n';
    }
  }
  return theSourceCode.replace( /,\n$/, '\n}' );
}

var theHash = { size:12, count:53, tax:0.06, sku:8295193 };

document.write( theHash.toSourceCode( ) );
Expected result:
{
  size : 12,
  count : 53,
  tax : 0.06,
  sku : 8295193
}
Result in your browser:
Result in Safari 2.0:
{
  toSourceCode : function () 
{
  var theSourceCode = "{
";
  for (var thePropName in this)
  {
    if (!this.hasOwnProperty || this.hasOwnProperty(thePropName))
    {
      theSourceCode += "    "+thePropName+" : "+this[thePropName]+",
";
    }
  }
  return theSourceCode.replace(,\n$, "
}");
},
  size : 12,
  count : 53,
  tax : 0.06,
  sku : 8295193
}       
Result in Firefox 1.0:
{
  size : 12,
  count : 53,
  tax : 0.06,
  sku : 8295193
}
Result in IE Mac 5.2.3:
{
  size : 12,
  count : 53,
  tax : 0.06,
  sku : 8295193,
  toSourceCode : function( )
{
  var theSourceCode = '{\n';
  for ( var thePropName in this )
  {
    if ( !this.hasOwnProperty || this.hasOwnProperty( thePropName ) )
    {
      theSourceCode += '  ' + thePropName + ' : ' + this[ thePropName ] + ',\n';
    }
  }
  return theSourceCode.replace( /,\n$/, '\n}' );
}
}
Result in iCab 2.9.7:
{
  size : 12,
  count : 53,
  tax : 0.06,
  sku : 8295193
}