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

});

});

相關文章
相關標籤/搜索