在创建一个对象的时候,js有三种可以选择的方法
1. Factory Pattern
function createCourse(number, room, teacher){ var course = new Object(); course.number = number; course.room = room; course.teacher = teacher; course.toString = function(){ return 'The course '+this.number+' is taken in room '+this.room+' by teacher '+this.teacher; }; course.showCourse = function(){ document.write(this); }; return course; } var c1 = createCourse(1, 'R110', 'Mike'); c1.showCourse();
2. Constructor Pattern
function Course(number, room, teacher){ this.number = number; this.room = room; this.teacher = teacher; this.toString = function(){ return 'The course '+number+' is taken in room '+room+' by teacher '+teacher; }; this.showCourse = function(){ document.write(this); }; }; var c2 = new Course(2,'T101', 'Matti'); c2.showCourse();
3. Prototype Pattern
function Course2(){ } Course2.prototype.number = 'No course number'; Course2.prototype.room = 'No room number'; Course2.prototype.teacher = 'No teacher'; Course2.prototype.toString = function(){ return 'The course '+this.number+' is taken in room '+this.room+' by teacher '+this.teacher; }; Course2.prototype.showCourse = function(){ document.write(this); }; var c3 = new Course2(); c3.showCourse();
Prototype Pattern在内存和资源的利用上要更有效些,prototype pattern可以给对象设置默认值和默认方法
“Another alternative is the Prototype Pattern. With this pattern, memory and resources are used more effeciently as multiple objects share resources. The Constructor fuction boyd is (typically) null. Prototypies and functions are assigned to the constructed object's prototype. Prototype is a default property within and object that contains properties and methods available to all instances of that object. This technique allows defining 'default values' for objects and object functions. The object creation and call does not specify property values." -- 《Advanced Topics in Web Development - Fall 2011》
相关推荐
Javascript 设计模式之构造函数模式.zip
首先介绍了什么是模式、模式的结构、类别、模式的分类、如何编写模式等等;然后,集中介绍了很多流行的设计模式在JavaScript中的应用,包括Module(模块)模式、Observer(观察者)模式、Facade(外观)模式和...
类式继承模式#2——借用构造函数 类式继承模式#3——借用和设置原型 类式继承模式#4——共享原型 类式继承模式#5——临时构造函数 Klass 原型继承 通过复制属性实现继承 借用方法 小结 第7章 ...
本书在尊重《设计模式》原意的同时,针对JavaScript语言特性全面介绍了更适合JavaScript程序员的了16个常用的设计...本书将教会你如何把经典的设计模式应用到JavaScript语言中,编写出优美高效、结构化和可维护的代码。
javascript组合使用构造函数模式和原型模式实例.docx
构造函数与字面量结合模式、动态原型模式、寄生构造模式、稳妥构造模式、借调函数。 用哪个就把哪个的注释打开哦!
JavaScript设计模式之构造器模式(生成器模式)定义与用法实例分析.docx
外文翻译:学用JavaScript设计模式,pdf版本,中文版 设计模式是可重用的用于解决软件设计中一般问题的方案。设计模式如此让人着迷,以至在任何编程语言中都有对其进行的探索。 其中一个原因是它可以让我们站在巨人...
算法导论 第三版 中文版 清晰 PDF,全书共8部分35章节,内容涵盖基础知识、排序和顺序统计量、数据结构、高级设计和分析技术、高级数据结构、图算法、算法问题选编、以及数学基础知识。非常实用的参考书和工程实践...
本书在尊重《设计模式》原意的同时,针对JavaScript语言特性全面介绍了更适合JavaScript程序员的了16个常用的设计...本书将教会你如何把经典的设计模式应用到JavaScript语言中,编写出优美高效、结构化和可维护的代码。
javascript工厂模式和构造函数模式创建对象方法解析.docx
javascript设计模式Constructor(构造器)模式.docx
深入理解JavaScript系列(26):设计模式之构造函数模式 深入理解JavaScript系列(27):设计模式之建造者模式 深入理解JavaScript系列(28):设计模式之工厂模式 深入理解JavaScript系列(29):设计模式之装饰...
主要介绍了javascript组合使用构造函数模式和原型模式的方法,通过一个简单实例分析了javascript构造函数模式与原型模式的使用方法,需要的朋友可以参考下
如果你希望学习如何写出漂亮的,具有结构性和组织性的代码,这本书就是为你而写!
深入理解JavaScript系列(26):设计模式之构造函数模式 深入理解JavaScript系列(27):设计模式之建造者模式 深入理解JavaScript系列(28):设计模式之工厂模式 深入理解JavaScript系列(29):设计模式之装饰...
全书共八章节,内容包括简介、基本技巧、字面量和构造函数、函数、对象创建模式、代码复用模式等。
类式继承模式#5——临时构造函数 Klass 原型继承 通过复制属性实现继承 借用方法 小结 第7章 设计模式 单体模式 工厂模式 迭代器模式 装饰者模式 策略模式 外观模式 代理模式 中介者模式 观察者模式 小结 第8章 DOM...