zoukankan      html  css  js  c++  java
  • 继承

    function Person(name,age,sex) {
    this.name=name;
    this.age=age;
    this.sex=sex;
    }
    Person.prototype.eat=function () {
    console.log("吃好吃的");
    };
    Person.prototype.play=function () {
    console.log("玩好玩的");
    };
    function Student(score) {
    this.score=score;
    }
    //改变学生的原型指向即可======》学生和人的关系已发生
    Student.prototype=new Person("小明",10,"男");
    Student.prototype.study=function () {
    console.log("学习很容易掉头发的。。。。");
    };
    //相同的代码太多,造成了代码的重复
    var stu=new Student(100);
    console.log(stu.name);
    console.log(stu.age);
    console.log(stu.sex);
    stu.eat();
    stu.play();
    console.log("下面是学生对象中自己有的");
    console.log(stu.score);
    stu.study();



    案例:
    * 动物有名字,有体重,有吃东西行为
    * 小狗有名字,有体重,有毛色,有吃东西行为,还有咬人行为
    * 哈士奇有名字,有体重,有毛色,性别,有吃东西行为,咬人行为和逗主人开心
    * */
    //动物的构造函数
    function Aimal(name,weight) {
    this.name=name;
    this.weight=weight;
    }
    //动物的原型方法
    Aimal.prototype.eat=function () {
    console.log("除了吃,还是吃");
    };
    //小狗的构造函数
    function Dog(color) {
    this.color=color;
    }
    //小狗的原型方法
    Dog.prototype=new Aimal("哮天犬","50");
    Dog.prototype.bitePerson=function () {
    console.log("汪汪汪,我会咬人的");
    };
    //哈士奇的构造函数
    function ErHa(sex) {
    this.sex=sex;
    }
    ErHa.prototype=new Dog("灰白色");
    ErHa.prototype.playHost=function () {
    console.log("我会咬鞋子,衣服还有沙发");
    };
    var reHa=new ErHa("雄性");
    console.log(reHa.name,reHa.weight,reHa.color);
    reHa.eat();
    reHa.bitePerson();
    reHa.playHost();
  • 相关阅读:
    JS运算符
    JS基础
    0507-弹性盒子
    0506css3(2D、3D)动画
    CSS3边框
    0503-格式与布局
    0502-边框边界
    0502-其他html标签
    0428-专题块状元素
    mysql 数据库学习
  • 原文地址:https://www.cnblogs.com/lujieting/p/10067305.html
Copyright © 2011-2022 走看看