你的位置:首页 > 软件开发 > ASP.net > 设计模式(二):构造器模式与模块模式

设计模式(二):构造器模式与模块模式

发布时间:2016-06-11 14:00:07
这一篇主要讲述构造器(Constructor)模式和模块(Module)模式以及相关的变体模式,例子是JavaScript代码。构造器(Constructor)模式对象构造器用于创建特定类型的对象——准备好对象以备使用,同时接收构造器可以使 ...

这一篇主要讲述构造器(Constructor)模式和模块(Module)模式以及相关的变体模式,例子是JavaScript代码。

构造器(Constructor)模式

对象构造器用于创建特定类型的对象——准备好对象以备使用,同时接收构造器可以使用的参数,以在第一次创建对象时,设置成员属性和方法的值。概念并没什么好说的,这种模式最是简单,虽然名字是那么吊炸天,但内容没什么,看下面例子就可明白。

基本构造器

function Car( model, year, miles ) { this.model = model; this.year = year; this.miles = miles; this.toString = function () {  return this.model + " has done " + this.miles + " miles"; };}// Usage:// We can create new instances of the carvar civic = new Car( "Honda Civic", 2009, 20000 );var mondeo = new Car( "Ford Mondeo", 2010, 5000 );// and then open our browser console to view the// output of the toString() method being called on// these objectsconsole.log( civic.toString() );console.log( mondeo.toString() );

原标题:设计模式(二):构造器模式与模块模式

关键词:设计模式

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。