We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
// 谷歌地图 /** * 最开始的情况* * / var googleMap = ()=> console.log('google'); var baiduMap =() => console.log('baiduMap'); var sogouMap = () => console.log('sogouMap'); var renderMap= (type)=>{ if (type == 'google') { googleMap() }else if(type== 'baidu'){ baiduMap() }else{ sogouMap() } } /** * 如果在增加一种情况需要改很多东西,renderMap 是需要改动的 写的太low的了 */ var googleMap ={ show() { console.log('谷歌'); } } var baiduMap = { show() { console.log('baidu'); } } var sogouMap={ show() { console.log('sugou'); } } // type 不要使用 不变的进行 不可变的进行隔离 隔离是相对的,�可变的进行封装; // 如果再多一种形式 ,不可变的隔离之后不能做任何的操作,分装组件也是 对于组件而言 可变的进行封装 var renderMap= (type)=>{ if (type == 'google') { googleMap.show() }else if(type== 'baidu'){ baiduMap.show() }else{ sogouMap.show() } } // renderMap('google') // renderMap('baidu') /** * 做什么 怎么去做是分开的; */ var renderMap=(map)=>{ map.show instanceof Function ? map.show() : null; } var renderMap =(map)=> map.show instanceof Function ? map.show() : null; renderMap(googleMap) PS:
var calculateBonus = (performanceLevel, salary) => { // 不可变进行隔离,可变的进行封装, // 如果在增加一种情况好增加吗? // 如果想复用这段代码如何能够复用。 能不能进封装; 如果可以独立得逻辑,抽离成函数 if (performanceLevel == 's') { return salary *5 } if (performanceLevel == 'a') { return salary * 3 //salary * 5 是算法是比较简单的 } } calculateBonus('s',4400); 设计模式 策略模式 var strategies = { 'a':(salary)=>(salary * 4), 'b':(salary)=>(salary * 3) } var calculateBonus = (level,salary ) =>(strategies[ level ]( salary )) 函数式 这些“算法”可以被封装到函数中并且四处传递,也就是我们常说的“高阶 函数”。 var S = function (salary) { return salary * 4; }; var A = function (salary) { return salary * 3; }; var B = function (salary) { return salary * 2; }; var calculateBonus = function (func, salary) { return func(salary); }; calculateBonus(S, 10000); 函数 vs 对象 var stages={ 's':salary=> salary*4, 'a':salary=> salary*3 } var fn = (type,salary)=>stages[type](salary) console.log(fn('s',2000)); // 函数式的作用是什么,写成函数到处传播 函数式更加好 var S = salary => salary*4; var fn = (S,salary)=>S(salary); console.log(fn(S,3000));
The text was updated successfully, but these errors were encountered:
赞
Sorry, something went wrong.
No branches or pull requests
设计模式 验证设计模式是否成功,复用和增加情况
将行为分布在各个对象中,并让这些对象各自负责自己的行为,这正是面向对象设计的优点。
项目实战
The text was updated successfully, but these errors were encountered: