javascript高级知识点——内置对象原型

代码信息来自于http://ejohn.org/apps/learn/。

可以修改内置对象的方法。

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){ 
  console.log( value, "Is in position " + index + " out of " + (array.length - 1) ); 
});

内置对象也继承自其原型,可以修改原型的方法,不过强烈不推荐这么做,因为很可能造成未知的错误。

原文地址:https://www.cnblogs.com/winderby/p/4074177.html