javascript的OO实现、继承
2010-01-10 01:17 fdafda 阅读(184) 评论(0) 收藏 举报javascript cero并没有OO方法,这里讨论的是prototype原声链方式,比较实用。
//创建一个类
var People = function(){}
//类方法
People.prototype = {
getName:function(){
alert(this.name || '不认识');
},
getAge:function(){
alert(this.age || '不晓得多大');
}
}
//继承类
var Children = function(){}
Children.prototype = new People();
//重写类方法
Children.prototype = {
getAge:function(){
if(this.age > 18){
alert('成年' || '不晓得');
}
else{
alert('幼齿' || '不晓得');
}
}
}
在使用时,必须使用new方法,例如:
var jim = new People(); jim.name = 'Jim'; var colin = new Children(); colin.age = 16; jim.getName(); colin.getAge();
浙公网安备 33010602011771号