saber 酱的抱枕

Fly me to the moon

04/20
2015
其他

js中使用prototype创建方法

prototype 属性不仅可以定义构造函数的属性和方法,还可以为本地对象添加属性和方法。它可以创建新方法,也可以重定义已有方法。例如:
var aColors=new Array("red","green","blue");
Array.prototype.findIndex=function (o) {
for (var i = 0; i < this.length; i++) { if(this[i]==o){ alert(o+'在本数组中的位置是'+i); return true; } } alert('在数组中没有'+o+'这个值!'); } aColors.findIndex('red'); aColors.findIndex('yellow');[/code] 查看w3c页面:ECMAScript 修改对象

js中使用prototype创建方法