Created
July 28, 2017 04:57
-
-
Save MrKou47/2a17d38d025253e7ae59e60515c29ea5 to your computer and use it in GitHub Desktop.
工厂模式
This file contains 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
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