数组扁平化
- 就是将一个 "多维数组" 转化成一个 "一维数组"
- 面试版
const arr = [1,2,3,4,[5,6,7,8,[9,10,11,12]]]
function flat(origin) {
const newArr = []
function fn(fnOrigin) {
fnOrigin.forEach((item) => {
if(Object.property.toString.call(item) === '[object Array]'){
fn(item)
} else {
newArr.push(item)
}
})
}
fn(origin)
return newArr
}
const flatArr = flat(arr)
console.log(flatArr)
- 工作版
console.log(arr)
console.log(arr.flat(1))
console.log(arr.flat(2))
console.log(arr.flat(Infinity))
设计模式
单例模式
- 一个构造函数,一生只能创建一个实例化对象
- 准备一个变量默认赋值为 null,然后在第一次实例化的时候,给这个变量赋值为实例化对象
- 后续在调用实例化的时候,就不再创建实例化对象了,而是拿到提前准备好的变量
class Dialog {
constructor () {
console.log('在页面中创建了一个弹出层')
}
}
const newDialog = (function () {
let instance = null
return function () {
if(instance === null) {
instance = new Dialog()
}
return instance
}
})()
const d1 = newDialog()
const d2 = newDialog()
单例模式升级
const Dialog = (function () {
let instance = null
class Dialog {
constructor(title) {
this.title = ''
console.log('在页面中创建了一个弹出层')
}
setTitle(newTitle) {
this.title = newTitle
}
}
return function (type) {
if(instance === null){
instance = new Dialog()
}
instance.setTitle(type)
return instance
}
})()
const d1 = new Dialog('警告')
console.log(d1)
const d2 = Dialog('通用')
console.log(d2)
策略模式
- 为了解决过多的 if... else 的嵌套问题
- 核心:
- 创建一个数据结构,这个结构内存储着各种折扣记录,对应的值 是这个折扣的计算方式
const calcPrice = (function () {
const calcList = {
'80%': (total) => {return (total * 0.8).toFixed(1)},
'70%': (total) => {return (total * 0.7).toFixed(1)},
}
function inner (type,total) {
return calcList[type](total)
}
return inner
})()
console.log(calcPrice('80%',500));
console.log(calcPrice('80%',562.2));
const calcPrice = (function () {
const calcList = {
'80%': (total) => {return (total * 0.8).toFixed(1)},
'70%': (total) => {return (total * 0.7).toFixed(1)},
'60%': total => (total * 0.6).toFixed(1),
}
function inner (type,total) {
return calcList[type](total)
}
inner.add = function (type,fn) {
calcList[type] = fn
console.log(calcList)
}
inner.sub = function (type) {
delete calcList[type]
console.log(calcList)
}
inner.getList = function() {
return calcList
}
return inner
})()
console.log(calcPrice('80%',500));
console.log(calcPrice('80%',562.2));
calcPrice.add('50%',total => (total * 0.5).toFixed(1))
calcPrice.add('88%',total => (total * 0.88).toFixed(1))
calcPrice.sub('88%')
发布订阅模式
- 一般用来处理 大型项目(框架)的封装
- 模拟发布订阅模式场景:
- 你去到书店,问店员 有没有一本书叫做 <JS 从入门到精通>
- 店员: 我们现在没有,然后你留下一个联系方式,将来有了之后我电话通知你
- 过了一段时间,有书本了,店员通过电话联系到你,触发了你的技能,然后你买了
class observer {
constructor (name) {
this.name = name
this.msgList = {}
}
add (type,fn) {
if(this.msgList[type] === undefined) {
this.msgList[type] = []
}
this.msgList[type].push(fn)
}
tri (type) {
this.msgList[type].forEach(item => item())
}
delName(type,fn) {
this.msgList[type] = this.msgList[type].filter(item => item !== fn)
}
}
const o1 = new observer('小刚')
console.log(o1)
const bookName1 = 'JS从入门导入图'
const bookName2 = '颈椎病预防'
const fnA = () => console.log('我是锅锅,我想买这本书')
const fnB = () => console.log('我是张三,我想买这本书')
const fnC = () => console.log('我是李四,我想买这本书')
o1.add(bookName1,fnA)
o1.add(bookName1,fnB)
o1.add(bookName1,fnC)
o1.delName(bookName1,fnB)
o1.tri(bookName1)
console.log('有预约者后的书店', o1)