原型继承、构造继承、拷贝继承

原型继承

代码如下:

    // 目的:让学生继承人的属性和方法(构造函数的和原型上的)、
    function Person(name, age) {
        this.name = name
        this.age = age
        this.eat = function () {
            console.log(this.name + '喜欢吃')
        }
    }
    Person.prototype.sex = '男'
    Person.prototype.sleep = function () {
        console.log(this.name + '喜欢睡觉')
    }
    function Student(code) {
        this.code = code
        this.play = function () {
            console.log(this.name + '喜欢玩游戏')
        }
    }
    Student.prototype.height = 100
    Student.prototype.study = function () {
        consoloe.log(this.name + '喜欢学习')
    }
    console.log(Student.prototype.constructor == Student)
    Student.prototype = new Person('小明', 18)
    // 将学学生原型的属性和方法放到继承里面
    Student.prototype.height = 100
    Student.prototype.study = function () {
        console.log(this.name + '喜欢学习')
    }
    var stu = new Student(100)
    console.log(stu)
    // 学生构造函数的属性和方法
    console.log(stu.code)
    stu.play()
    // 学生原型上的属性和方法
    console.log(stu.height)
    stu.study()
    // 人构造函数的属性和方法
    console.log(stu.name)
    stu.eat()
    // 人原型上的属性和方法
    console.log(stu.sex)
    stu.sleep()

构造继承

代码如下:

    // 目的:让学生继承人的属性和方法(构造函数的和原型的)
    function Person(name, age) {
        this.name = name
        this.age = age
        this.eat = function () {
            console.log(this.name + '喜欢吃')
        }
    }
    Person.prototype.sex = '男'
    Person.prototype.sleep = function () {
        console.log(this.name + '喜欢睡觉')
    }
    function Student(code, x, y) {
        this.code = code
        this.paly = function () {
            console.log(this.name + '喜欢玩游戏')
        }
        Person.call(stu)
        Person.call(this, x, y)
    }

    Student.prototype = new Person()
    Student.prototype.height = 100
    Student.prototype.study = function () {
        console.log(this.name + '喜欢学习')
    }
    var stu = new Student(100, '小明', 18)
    var p = new Person('小明', 18)
    console.log(stu)
    // 学生构造函数的属性和方法
    console.log(stu.code)
    stu.paly()
    // 学生原型的属性和方法
    console.log(stu.height)
    stu.study()
    // 人构造函数的属性和方法
    console.log(stu.name)
    stu.eat()
    // 人原型的属性和方法
    console.log(stu.sex)
    stu.sleep

拷贝继承

代码如下:

    // 目的:让学生继承人的属性和方法(构造函数的和原型的)
    function Person(name, age) {
        this.name = name
        this.age = age
        this.eat = function () {
            console.log(this.name + '喜欢吃')
        }
    }
    Person.prototype.sex = '男'
    Person.prototype.sleep = function () {
        console.log(this.name + '喜欢睡觉')
    }
    function Student(code, x, y) {
        this.code = code
        this.play = function () {
            console.log(this.name + '喜欢玩游戏')
        }
    }
    Student.prototype.height = 100
    Student.prototype.study = function () {
        console.log(this.name + '喜欢学习')
    }
    var stu = new Student(100)
    var p = new Person('小明', 18)
    for (x in p) {
        stu[x] = p[x]
    }
    console.log(stu)
    // 学生构造函数的属性和方法
    console.log(stu.code)
    stu.play()
    // 学生原型上的属性和方法
    console.log(stu.height)
    stu.study()
    // 人构造函数的属性和方法
    console.log(stu.name)
    stu.eat()
    //  人原型上的属性和方法
    console.log(stu.sex)
    stu.sleep()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值