• 61.工厂/原型/构造


    // 工厂
            function Person1(name, age, job) {
                let p = {};
                p.name = name;
                p.age = age;
                p.job = job;
                p.say = function () {
                    console.log('my name is :' + this.name)
                }
                return p
            }
            let p1 = Person1('ll', 55, 'ss');
            p1.say();
            let p2 = Person1('mm', 55, 'ss');
            p2.say();
            // 构造
            function Person(name, age, job) {
                this.name = name;
                this.age = age;
                this.job = job;
                this.say = function () {
                    console.log('my name is :' + this.name)
                }
            }
            let person1 = new Person('lily', 18, 'net');
            person1.say();
            // 原型
            function Person1() { }
            Person1.prototype = {
                name: 'lily',
                brother: ['ll', 'jj', 'mm'],
                age: 19,
                say: function () {
                    console.log('~!!!~~~~~~')
                    console.log('my name is :' + this.name);
                    console.log('my brother ' + this.brother)
                }
            }
            let person2 = new Person1();
            let person22 = new Person1();
            person2.name = 'john';
            person2.brother.push('cc');
            person2.hi = function () {
                console.log('my name is:' + this.name + ' and ' + this.age + ' years old')
            }
            person2.say();
            person22.say();
            person2.hi();
            // 构造原型混合
            function Person3(name, age, job) {
                this.name = name;
                this.age = age;
                this.job = job;
                this.brother = ['kk', 'll', 'mm'];
            }
            Person3.prototype = {
                constructor: Person3,
                say: function () {
                    console.log('my name is: ' + this.name)
                    console.log('my brother~~>', this.brother)
                }
            }
            let person3 = new Person3('lilei', 22, 'mmp');
            let person4 = new Person3('hack', 22, 'mmp');
            person3.brother.push('cxv');
            person3.say();
            person4.say();
            // 动态原型
            function Person4(name, age, job) {
                this.name = name;
                this.age = age;
                this.job = job;
                this.brother = ['kk', 'll', 'mm'];
                if (typeof this.say != 'function') {
                    Person4.prototype.say = function () {
                        console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
                        console.log('my name is :' + this.name)
                        console.log('my brother~~>', this.brother)
                    }
                }
            }
            let person5 = new Person4('bob', 23, 'teacher');
            person5.brother.push('nnnnnnnnn')
            person5.say();
            let person6 = new Person4('bob', 23, 'teacher');
            person6.say();
            // 原型链继承
            function Person5(name) {
                this.name = name
            }
            Person5.prototype = new Person4('hh', 11, 'kk');
            console.log('~~~~~~~~~原型链继承~~~~~~~~~~');
            let PPPP5 = new Person5('jjjjj')
            PPPP5.say();
    

      

  • 相关阅读:
    Mysql索引查询失效的情况
    常用的设计模式
    dubbo的实现原理
    HashMap和HashTable的区别
    SpringMVC工作原理的介绍
    SpringMVC 基础内容及使用步骤
    BeanFactory和ApplicationContext的区别+部分Spring的使用
    Spring常用的jar+普通构造注入
    如何在CentOS7上安装MySQL并实现远程访问
    如何搭建Spring MVC 框架---Hello World
  • 原文地址:https://www.cnblogs.com/famLiu/p/10872259.html
Copyright © 2020-2023  润新知