Skip to content

Instantly share code, notes, and snippets.

@linx4200
Created March 17, 2018 12:51
Show Gist options
  • Save linx4200/610c5b50f58b8632146da137975ff3bf to your computer and use it in GitHub Desktop.
Save linx4200/610c5b50f58b8632146da137975ff3bf to your computer and use it in GitHub Desktop.
【Factory】最简单的工厂模式
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