JS---继承

function Person (name,age) {
            this.name=name;
            this.age=age;
            this.hi1=function() {
                console.log('name:'+this.name+',age:'+this.age+'.');
            }
        }
        Person.prototype.hi = function() {
            console.log('my name is '+this.name+','+this.age+' years old.');
        };
        julia=new Person('julia',9);
        julia.hi1();
        julia.hi();

        function Student (name,age,grade) {
            Person.call(this,name,age);
            this.grade=grade;
        }

        mike=new Student('mike',10,'grade 4');
        console.log(mike.name,mike.age,mike.grade);
        mike.hi1();//可以调用
        //mike.hi();//不能执行,
        //在Student构造函数里,调用了Person构造函数.所以Student实例能调用Person构造函数里的方法,但不能调用Person实例原型(即Person.Prototype属性)里定义的方法。

        Student.prototype=Object.create(Person.prototype);
        Student.prototype.construct=Student;

        Student.prototype.hi=function() {
            console.log('my name is '+this.name+','+this.age+' years old,in '+this.grade+'.');
        }

        Student.prototype.learn=function(subject) {
            console.log(this.name+' is learning '+subject+' in '+this.grade+'.')
        }
        jone=new Student('jone',11,'grade 5');
        jone.hi1();
        jone.hi();
        jone.learn('math');

转载于:https://www.cnblogs.com/beast-king/p/5264993.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值