JavaScript Extending Prototypes #81 from John Resig learning advanced JavaScript
<p>The is example #81 from John Resigs Learning Advanced JavaScript, called Beware: Extending Prototypes Can Be Dangerous.http://ejohn.org/apps/learn/#81  As it is written (length == 3), the assert fails, but if you assert <code>obj.keys().length == 4</code> it passes. </p>

<p>Here are some questions</p>

<p>1) Why is he saying "We should only have 3 properties"? In what way was it unexpected or disappointing to have 4 properties?</p>

<p>2) How does it get 4 properties?</p>

<p>3) What is <code>var i</code> in the third line of the program?</p>

<p>4) Does <code>this</code> in the third line refer to Object?</p>

<p>5) Looking at the assert, does this code <code>obj.keys().length</code>  call the function in the first line?</p>

<p>6) If there are 4 properties in obj.keys().length, are there also 4 elements in the returned array var keys once it is returned? </p>

<pre><code>Object.prototype.keys = function(){
  var keys = [];
  for ( var i in this )
    keys.push( i );
  return keys;
};

var obj = { a: 1, b: 2, c: 3 };

assert( obj.keys().length == 3, "We should only have 3 properties." );

delete Object.prototype.keys;
</code></pre>
