JavaScript中的多态和继承

82 阅读1分钟

多态

个人笔记,方便自己看,另外感谢后盾人向军老师。本笔记是通过观看向军老师视频得出。

根据多种不同的形态产生不同的结果就是多态

        function User() { };
        User.prototype.show = function () {
            this.description();
        }
​
        function User1() { };
        User1.prototype = Object.create(User.prototype);
        User1.prototype.description = function () {
            console.log('User1');
        }
        function User2() { };
        User2.prototype = Object.create(User.prototype);
        User2.prototype.description = function () {
            console.log('User2');
        }
        function User3() { };
        User3.prototype = Object.create(User.prototype);
        User3.prototype.description = function () {
            console.log('User3');
        }
        for (const a of [new User1, new User2, new User3]) {
            a.show();//User1
            //User2
            //User3
        }

同一个函数的执行,产生三个不同的结果

利用父类构造函数初始化属性
function User(name, age) {
            this.name = name;
            this.age = age;
        };
        User.prototype.show = function () {
            console.log(this.name, this.age);
        }
​
        function User1(name, age) {
            User.call(this, name, age);
        }
        User1.prototype = Object.create(User.prototype);
        let why = new User1('why', 19);
        why.show()
​
        function User2(...args) {
            User.apply(this, args);
        }
        User2.prototype = Object.create(User.prototype);
        let wy = new User2('wy', 18);
        wy.show()
        // 封装一个extend函数,用于子函数继承父函数
        function extend(child, parent) {
            child.prototype = Object.create(parent.prototype);
            child.prototype.constructor = child;
            Object.defineProperty(child.prototype, 'constructor', {
                value: child,
                enumerable: false,
            })
        }
对象工厂派生对象并实现继承
        function User(name, age, sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;
        };
        User.prototype.show = function () {
            console.log(this.name, this.age);
        }
        function User1(name, age, sex) {
            const stance = Object.create(User.prototype);
            User.call(stance, name, age, sex);
            stance.Issex = function () {
                console.log(this.sex === 'man' ? 'man' : 'woman');
            }
            return stance;
        }
        let why = new User1('why', 19, 'man');
        console.dir(why)
        why.show(); //why,19
        why.Issex(); // man
多继承Object.assgin()
 function User(name, age = 19) {
            this.name = name;
            this.age = age;
        };
        User.prototype.show = function () {
            console.log(this.name, this.age);
        }
​
        let User1 = {
            sayOne() {
                console.log('there is One,your name is ' + this.name);
            }
        }
        let User2 = {
            sayTwo() {
                console.log('there is Two,your name is ' + this.name);
            }
        }
        let User3 = {
            sayThree() {
                console.log('there is Three,your name is ' + this.name);
            }
        }
        function Customer(name) {
            let constance = Object.create(User.prototype);
            User.call(constance, name)
            return constance;
        }
        // 如果实例想使用sayOne,可以将将sayOne函数设为User的原型,但使用不同对象的方法越多,继承就更为混乱,所以
        // 多继承使用属性合并,将构造函数改为对象。
        Customer.prototype = Object.assign(User.prototype, User1, User2, User3)
        let wy = new Customer('wy', 18);
        wy.sayOne() //there is One,your name is wy
        wy.sayThree() //there is Three,your name is wy
        wy.show() //wy 19

\