Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Lelana villa
Added: Mar 23, 2018 7:43 PM
Views: 24
Tags: no tags
And now every array in our global scope has access to the first() method. This may be convenient in some situations, but is generally a bad idea. If you modify a prototype, there is always the possibility to clash with other code from your program.
  1. var arr = [2, 2, 3, 4]
  2.  
  3. Array.prototype.first = function() {
  4. return this[0];
  5. }
  6.  
  7. console.log(arr.first()); // prints 1