定义类
function People() {
this.eat = function (food) {
alert("你想要吃: " + food);
}
}
1、为用户自定义类添加方法:
People.prototype.sleep = function (time) {
alert("每个人每天最少需要睡 " + time+"小时");
};
2、为脚本环境内建对象添加方法:获取数组中的最大值
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;
};
3、更新自定义类的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);
});
});