js继承基础解析

博客介绍了JavaScript的多种继承方式,包括原型继承、组合继承、寄生式继承和ES6的class继承。详细给出了各继承方式的代码示例,并对原型继承的缺点及组合继承的改进方法进行说明,帮助理解不同继承方式的实现原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原型继承:

        function Person(name,age){
                this.name=  name;
                this.age = age;
            }
            Person.prototype.sayHello=function(){
                console.log(this.name);
            }
            var person = new Person("zs",10);
            person.sayHello();

解释:原型对象上的属性和方法能够被实例访问到

 

    function Person(name,age){
                this.name= name;
                this.age=age;
            }
            Person.prototype.sayHello=function(){
                console.log(this.name);
            }
            function Male(){
            }
            Male.prototype=new Person("张三",10);//Male的原型对象等于peson的实例
            var male = new Male();
            male.sayHello();

解释:male上有指Male原型对象的内部指针,Male的原型对象上有指向Persn原型对象的内部指针。

缺点:每一个的实例只有相同的属性值

组合继承:

function Animal(name,age){
                this.name = name;
                this.age = age;
            }
            Animal.prototype.sayHello=function(){
                console.log("aaa");
            }
            Animal.prototype.sayHello=function(){
                console.log("bb");
            }
            function Cat(name,age){
                Animal.call(this,name,age);//属性继承
            }
            function Dog(name,age){
                Animal.call(this,name,age);
            }
//            Cat.prototype=Animal.prototype;//赋值赋 的是地址  所以父类能访问到子类的方法
//            var cat = new Cat();
//            cat.sayHello();

    //改进方法
            for(var i in Animal.prototype){
                Cat.prototype[i]=Animal.prototype[i];//原型方法继承
            }
            Cat.prototype.sayHi =function(){
                console.log("小猫");
            }
            var cat = new Cat();
            var animal = new Animal();
            cat.sayHello();
            cat.sayHi();
            animal.sayHi();//

寄生式继承

function Perosn(name,age){
                this.name = name;
                this.age = age;
            }
            function Male(name,age){
                Person.call(this,name,age);
            }
            function inhert(Sub,Super){
                Sub.prototype=Object.create(Super.prototype);
                Sub.prototype.constructor=Sub;//更改构造函数的指向    构造函数里有原型对象,原型对象有指构造函数的指针
            }
            
            inhert(Male,Person);
            var male = new Male("20",10);

class继承     es6里的继承

class Person{
                constructor(name,age){
                    this.name=name;
                    this.age=age;
                }
            }
            class Male extends Person{
                constructor(name,age,sexy){
                    super(name,age,sexy);
                    this.sexy=sexy;
                }
            }
            var male = new Male("z",1,"男");
            console.log(male.name,male.age,male.sexy);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值