js的设计模式
pleasestartyourperformance / js-design-mode Goto Github PK
View Code? Open in Web Editor NEWjs的设计模式
js的设计模式
// #简单工厂模式第一种
/**
* 足球类
*/
var FootBall = function () {
this.play = function () {
console.log('我在踢足球');
}
}
/**
* 篮球类
*/
var BasketBall = function () {
this.play = function () {
console.log('我在打篮球');
}
}
var football = new FootBall();
football.play();
var basketball = new BasketBall();
basketball.play();
/**
* 球类工厂
*/
var Ball = function(name) {
switch (name) {
case '足球':
return new FootBall();
break;
case '篮球':
return new BasketBall();
break;
}
}
var football = Ball('足球');
football.play();
var basketball = Ball('篮球');
basketball.play();
// #简单工厂模式第一种end
这段案例可以简单的这么去理解,假设我们需要多个球,我们希望在使用球
的时候,只需要告诉管理员我们需要的球的类型,不需要一个个去找对应的球
这个管理员就相对于工厂函数。
简单讲就是使用简单工厂模式,那么你就不需要关心它的具体实现,
你只需要知道你要使用的类型,那么工厂函数会自动帮你去做对应的事情
原文连接:https://www.jianshu.com/p/0eed7edc93c0
github:https://github.com/ToNiQian/js-design-pattern/blob/master/demo1/index.js
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.