var Car = (function () { var Car = function (model, year, miles) { this.model = model; this.year = year; this.miles = miles; }; return function (model, year, miles) { return new Car(model, year, miles); }; })(); var tom = new Car("Tom", 2009, 20000);
如果还不理解的话,那我们就再详细一点咯,假如我们想在网页面里插入一些元素,而这些元素类型不固定,可能是图片,也有可能是连接,甚至可能是文本,根据工厂模式的定义,我们需要定义工厂类和相应的子类,我们先来定义子类的具体实现(也就是子函数):
var page = page || {}; page.dom = page.dom || {}; //子函数1:处理文本 page.dom.Text = function () { this.insert = function (where) { var txt = document.createTextNode(this.url); where.appendChild(txt); }; }; //子函数2:处理链接 page.dom.Link = function () { this.insert = function (where) { var link = document.createElement(‘a‘); link.href = this.url; link.appendChild(document.createTextNode(this.url)); where.appendChild(link); }; }; //子函数3:处理图片 page.dom.Image = function () { this.insert = function (where) { var im = document.createElement(‘img‘); im.src = this.url; where.appendChild(im); }; };
如何定义工厂处理函数呢?其实很简单:
page.dom.factory = function (type) { return new page.dom[type]; }
使用方式如下:
var o = page.dom.factory(‘Link‘); o.url = ‘http://www.cnblogs.com‘; o.insert(document.body);
原文:http://www.cnblogs.com/Watcher/p/3605389.html