1.简单工厂模式,又称为静态工厂方法,单一种类的复制使用函数
// 简单工厂函数 封装 动物this
function A(name, six) { var o = new Object() o.name = name o.six = six o.sayName = function () { console.log(this.name) } return o } var A1 = new A('大象', '公') var A2 = new A('狮子', '母') A1.sayName() // 大象 A2.sayName() // 狮子
2.工厂方法,我理解为同类产品下,定义一个大工厂,能够插入不少的小工厂来实现方法,小工厂之间能够很好的耦合prototype
// // 工厂方法code
var a = [{ name: '鸭子', type: '吃草' },{ name: '鸡', type: '吃肉' }] function B(type,chi) { B.prototype.chicken(type)
}继承
B.prototype = { duck: function () { }, chicken: function (type) { this.type = type console.log(this.type) } } for(var i =0;i<a.length;i++){ B(a[i].name,a[i].type) console.log(123) }
3.抽象工厂,能够生产产品族的工厂,例如:大天然 (大工厂类),无脊椎动物,脊椎动物,哺乳动物(能够称之为小工厂类),而这个小工厂类下面又能够分为不少动物种类(名字...等),它们之间生存的法则不同,有吃草的,有吃肉的。。。就像下面同样,每一个小工厂有本身的方法,干本身的事情互不干扰,没事我还能够继承一下肉食动物吃一下草,也是有可能的,开个玩笑!!()get
//抽象 制造一个动物种类原型
var zoom= function (fun,fun2) { function c() {} c.prototype = new zoom[fun2]() // 建立实例 fun.constructor =fun //构造器指向 // c函数赋予子类原型 fun.prototype = new c() } //无脊椎动物大类 zoom.Invertebrates = function () { this.type = 'Invertebrates' } zoom.Invertebrates.prototype = { getzoom: function () { return new Error('抽象方法不能调用!'); } } // 原生生物类 var native = function (name,num) { this.name = name this.num = num } zoom(native,'Invertebrates'); native.prototype.getzoom = function(){ console.log(this.name); } var native1 = new native('水母', 20000000000) native1.getzoom()