prototype的理解及实例

定义类数组

function People() {ide

this.eat = function (food) {this

alert("你想要吃: " + food);spa

      } prototype

}对象

一、为用户自定义类添加方法:ci

People.prototype.sleep = function (time) {it

alert("每一个人天天最少须要睡 " + time+"小时");io

};table

二、为脚本环境内建对象添加方法:获取数组中的最大值

Array.prototype.Max = function () {

var max = this[0];

for (var i = 1; i < this.length; i++) {

if (max < this[i]) {

max = this[i];

}

}

return max;

};

三、更新自定义类的prototype

function Man(){

      this.sleep=function(){

      alert("每一个人天天最少须要睡6小时");

}

}

function HumanRace(){

this.sport=function(){

alert("为老保持身体健康,请常常运动");

}

}

HumanRace.prototype=new Man();

 

 
 

$(document).ready(function () {

var pe = new People();

var man=new Man();

var human=HumanRace();

man.sleep();

human.sleep();

human.sport();

pe.eat("苹果");

pe.sleep("6");

jQuery("button").click(function () {

var array = [1, 4, 6, 7, 9];

var t = array.Max();

alert(t);

});

});

相关文章
相关标签/搜索