Inheritance javascript 继承的魅力

2011-08-14 08:57:28 by 【6yang】, 248 visits, 收藏 | 返回

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Inheritance javascript 继承的魅力</title>
</head>

<body>
<script type="text/javascript">
var Mammal = function( name ){
    this.name = name;
    this.sex = 'female';
    this.says = function(){
        return this.saying || '';
    };
    this.get_sex = function(){
        return this.sex;
    };
}
Mammal.prototype.get_name = function(){
    return this.name;
}
Mammal.prototype.says = function(){
    return this.saying || '';
};
Mammal.prototype.get_sex = function(){
    return this.sex;
};
 
//var myMammal = new Mammal();



var Cat = function(name){
    Mammal.apply(this,arguments);
    this.name = name;
    this.saying = 'jack';
    this.sex = 'male';
};
//Cat.prototype = new Mammal();
//Cat.prototype = Mammal.prototype;
Cat.prototype.purr = function(){
    var i, s = '';
    return i;
}
var myCat = new Cat('Emily');
var says = myCat.says();
alert(says);

alert(myCat.get_sex());
</script>
</body>
</html>

分享到:
share

    图片原图

    loading

    loading