Built-in Prototypes


We can also modify built-in object prototypes.
if (!Array.prototype.forEach) {
   Array.prototype.forEach = function(fn){
     for ( var i = 0; i < this.length; i++ ) {
       fn( this[i], i, this );
     }
   };
}
 
["a", "b", "c"].forEach(function(value, index, array){
   assert( value, "Is in position " + index + " out of " + (array.length - 1) );
});
Beware: Extending prototypes can be dangerous.
 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;[/c
  

你可能感兴趣的:(java,C++,c,C#,prototype)