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) ); 
});

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

posted on 2014-11-04 17:11  吹过的风  阅读(168)  评论(0编辑  收藏  举报