博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
创建对象
阅读量:4493 次
发布时间:2019-06-08

本文共 2866 字,大约阅读时间需要 9 分钟。

工厂模式

缺陷:无法知道对象类型

console.log("工厂模式");//无法识别对象function createPerson(name, age, job){    var o = new Object();    o.name = name;    o.age = age;    o.sayName = function(){        console.log(this.name);    };}var person1 = createPerson("Nicholas", 29, "Software Engineer");var person2 = createPerson("Greg", 27, "Doctor");

构造函数模式

缺点:方法不共享

new关键字会有以下4个步骤:
1)创建一个对象
2)将构造函数的作用域赋给新对象(因此this就指向该新对象)
3)执行构造函数中的代码
4)返回新对象

console.log("构造函数模式");//方法不共享,都有自己的方法function Person(name, age, job){    this.name = name;    this.age = age;    this.job = job;    this.sayName = function(){        console.log(this.name);    };}var person1 = new Person("Nicholas", 29, "Software Engineer");var person2 = new Person("Greg", 27, "Doctor");console.log(person1.constructor == Person); //trueconsole.log(person1 instanceof Object); //trueconsole.log(person1 instanceof Person); //true

原型模式

缺点:属性共享,属性不应该共享

console.log("原型模式");//属性共享,属性不应该共享function Person(){}Person.prototype.name = "Nicholas";Person.prototype.age = 29;Person.prototype.job = "Software Engineer";Person.prototype.sayName = function(){    console.log(this.name);};var person1 = new Person();person1.sayName();  //Nicholasvar person2 = new Person();person2.sayName();  //Nicholasconsole.log(person1.sayName == person2.sayName);    //true

组合使用构造模式和原型模式

认可度最高的一种模式

console.log("组合使用构造模式和原型模式");//认同度最高的构造模式function Person(name, age, job){    this.name = name;    this.age = age;    this.job = job;    this.friends = ["Shelby", "Court"];}Person.prototype = {    // body...    constructor:Person,    sayName:function(){        console.log(this.name);    }};var person1 = new Person("Nicholas", 29, "Software Engineer");var person2 = new Person("Greg", 27, "Doctor");person1.friends.push("Van");console.log(person1.friends);   //[ 'Shelby', 'Court', 'Van' ]console.log(person2.friends);   //[ 'Shelby', 'Court' ]console.log(person1.friends === person2.friends);   //falseconsole.log(person1.sayName === person2.sayName);   //true

动态原型模式

console.log("动态原型模式");function Person(name, age, job){    this.name = name;    this.age = age;    this.job = job;    if (typeof this.sayName != "function"){        Person.prototype.sayName = function() {            // body...            console.log(this.name);        };    }}var friend = new Person("Nicholas", 29, "Software Engineer");friend.sayName();

寄生构造函数模式

>代码上和工厂方式区别小,方式上区别console.log("寄生构造函数模式");function Person(name, age, job){    var o = new Object();    o.name = name;    o.age = age;    o.job = job;    o.sayName = function(){        console.log(this.name);    }    return o;}var friend = new Person("Nicholas", 29 ,"Software Enginner");friend.sayName();

稳妥构造函数模式

没有this关键字

console.log("稳妥构造函数模式");function Person(name, age, job){    var o = new Object();    o.sayName = function(){        console.log(name);    };    return o;}

转载于:https://www.cnblogs.com/xigui/p/5507889.html

你可能感兴趣的文章
设计模式之单例设计模式
查看>>
异常的基本概念
查看>>
vue 在发送axios请求时数据渲染问题
查看>>
动态链接库dll
查看>>
2018 Multi-University Training Contest 3 - HDU Contest
查看>>
组合数取模(转载)
查看>>
9.2NOIP模拟题
查看>>
整合SpringDataJpa
查看>>
vue过渡
查看>>
tcpreplay 博客目录
查看>>
oracle11g忘记sys密码
查看>>
文件各种上传,离不开的表单
查看>>
mysql查询插入优化
查看>>
hadoop备战:yarn框架的搭建(mapreduce2)
查看>>
微信公众号开发模式开启总结
查看>>
pygame-KidsCanCode系列jumpy-part2-加速度与摩擦力
查看>>
[elk]logstash的grok匹配逻辑grok+date+mutate
查看>>
准备Android面试
查看>>
界面与后台逻辑完全分离,单例模式,接口
查看>>
redis安装及测试
查看>>