day33 JS里面的继承

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

</body>
</html>
<script>
    //JS的继承是通过改变原型对象的指向实现的
    //    function Person(name,age,sex){
    //        this.name=name;
    //        this.age=age;
    //        this.sex=sex;
    //    }
    //    Person.prototype.eat=function(){
    //        console.log("吃饭饭?");
    //    }
    //    function Student(){};
    //    Student.prototype = new Person("秀秀",15,"女");
    //    var stu= new Student();
    //    console.log(stu);
    //    stu.eat();


    //    function Animal (name,pang){
    //        this.name=name;
    //        this.pang=pang;
    //    }
    //    Animal.prototype.eat=function(){
    //        console.log("吃肉或者吃素或者两种都吃");
    //    }
    //    function Dog (){};
    //    Dog.prototype= new Animal("柯基","20kg");
    //    Dog.prototype.jiao =function(){
    //        console.log("七夕来了,可以汪汪汪一天了");
    //    }
    //    function Keji(){};
    //    Keji.prototype=new Dog();
    //    Keji.prototype.eating=function (){
    //        console.log("七夕狗粮管够,随便吃");
    //    }
    //
    //    var keji = new Keji();
    //    console.log(keji.name);
    //    console.log(keji.pang);
    //    keji.eating();
    //    keji.eat();
    //    keji.jiao();


    //
    //    function Animal (name,pang){
    //        this.name=name;
    //        this.pang=pang;
    //    }
    //    Animal.prototype.eat=function(){
    //        console.log("吃肉或者吃素或者两种都吃");
    //    }
    //    function Dog (name,pang){
    //        Animal.call(this,name,pang);
    //    };
    //call Dog里面的name 和 pang 属性 继承自 Animal中;
    //call 只能继承属性  不能 继承方法








    //组合继承 就是把call方法 和 prototype 用到一起
//    function Animal(name, pang) {
//        this.name = name;
//        this.pang = pang;
//    }
//    Animal.prototype.eat = function () {
//        console.log("吃肉或者吃素或者两种都吃");
//    }
//    function Dog(name, pang) {
//        Animal.call(this, name, pang);
//    }
//    ;
//    Dog.prototype = new Animal("1", "2");
//    Dog.prototype.eat1 = function () {
//        console.log("ssss");
//    }
//    var dog = new Dog();
//    dog.eat1();
//    dog.eat();




    //拷贝继承
    function Person(){};
    Person.prototype.name="小明";
    Person.prototype.age=21;
    Person.prototype.sex="男";
    Person.prototype.show=function(){
        console.log("七夕将至,狗粮管够");
    }
    var obj={};
    for(var key in Person.prototype){
        obj[key]=Person.prototype[key];
    }
    console.log(obj);














</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值