js 继承的方式

//定义object的继承方法
            Object.extend = function(destination, source) {
                for(property in source) {
                    destination[property] = source[property];
                }
                return destination;
            }
            Object.prototype.extend = function(object) {
                return Object.extend.apply(this, [this, object]);
            }
            //声明动物类
            function Animail(name, age) {
                this.name = name;
                this.age = age;
                this.say = function() {
                    console.log("我是:" + this.name + "--年龄:" + this.age);
                }
            }
            const dog = new Animail("小狗", 12);
            dog.say();

            function Car(name, age) {
                //使用call实现继承Animail类
                //    Animail.call(this, name, age);
                //使用apply实现继承Animail类
                /*Animail.apply(this, arguments);
                this.run = function() {
                    console.log("车跑...")
                }*/
                this.name = name;
                this.age = age;
            }
            //使用object实现继承animail类
            Car.prototype = (new Animail).extend({
                //重写say方法
                say: function() {
                    console.log("我是车-名称:" + this.name + "age:" + this.age);
                },
                //添加 跑方法
                run: function() {
                    console.log("车running...");
                }
            });
            const caChe = new Car("卡车", 2);
            caChe.say();
            caChe.run();

            const qiChe = new Car("汽车", 15);
            qiChe.say();
            qiChe.run();

 es6 继承的实现

//定义animail类
            class Animail {
                //构造函数
                constructor() {};

                //属性的封装
                setName(name) {
                    this.name = name;
                };
                getName() {
                    return this.name;
                };
                setAge(age) {
                    if(age > 100 || age < 1) {
                        this.age = 1;
                    } else {
                        this.age = age;
                    }
                };
                getAge() {
                    return this.age;
                }
                //定义say的方法
                say() {
                    console.log("我是:" + this.name + "--age:" + this.age);
                };
                //静态方法
                static sayHello() {
                    console.log("我是静态方法!");
                }
            }
            //实例化dog
            const dog = new Animail();
            //设置name属性
            dog.setName("小狗");
            //设置年龄属性
            dog.setAge(1000);
            //d调用say方法
            dog.say();
            //获取属性的值
            console.log(dog.getName());
            //调用静态方法(静态方法不能被实例化对象调用)
            Animail.sayHello();

            //继承
            class People extends Animail {
                sayHello() {
                    console.log("大家好,我是:" + this.name + ",今年:" + this.age + "岁了!,我现在上" + this.grade + ",在" + this.className);
                };
                //年级名称
                setClassName(className) {
                    this.className = className;
                };
                getClassName() {
                    return this.className;
                };
                setGrade(gradeName) {
                    this.grade = gradeName;
                };
                getGrade() {
                    return this.grade;
                }
            }
            const xm = new People();
            xm.setName("小明");
            xm.setAge(10);
            xm.setClassName("二班");
            xm.setGrade("三年级");
            xm.say();
            xm.sayHello();
原文地址:https://www.cnblogs.com/lgjc/p/9364432.html