Skip to content

Instantly share code, notes, and snippets.

@MrKou47
Created July 28, 2017 04:57
Show Gist options
  • Save MrKou47/2a17d38d025253e7ae59e60515c29ea5 to your computer and use it in GitHub Desktop.
Save MrKou47/2a17d38d025253e7ae59e60515c29ea5 to your computer and use it in GitHub Desktop.
工厂模式
let carFactory = (function (name) {
function Benz() {
this.name = 'benz';
}
function BMW() {
this.name = 'bmw';
}
return function(name) {
switch (name) {
case 'benz':
return new Benz();
case 'bmw':
return new BMW();
default:
console.warn('car factory required an car name!');
break;
}
}
})();
let bmw = carFactory('bmw');
console.log(bmw);
let lincoln = carFactory('lincoln');
console.log(lincoln);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment