Created
March 17, 2018 12:51
-
-
Save linx4200/610c5b50f58b8632146da137975ff3bf to your computer and use it in GitHub Desktop.
【Factory】最简单的工厂模式
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var BasketBall = function () { /* ... */}; | |
var FootBall = function () { /* ... */}; | |
var Tennis = function () { /* ... */}; | |
// 运动工厂 | |
var sportsFactory = function (name) { | |
switch(name) { | |
case 'NBA': | |
return new BasketBall(); | |
case 'WorldCup': | |
return new FootBall(); | |
case 'ATP': | |
return new FootBall(); | |
} | |
} | |
// 另外一种工厂模式 | |
function createBook(name, time, type) { | |
// 创建一个对象,并对对象拓展属性和方法 | |
var o = new Object(); | |
o.name = name; | |
o.type = type; | |
o.time = time; | |
o.getName = function () { | |
console.log(this.name); | |
} | |
return o; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment