适配器模式
如下方法,由于参数太多,导致使用很麻烦,顺序也难以记忆
function doSomeThing(name, title, age, color, size, prize){}
所以我们可以采用对象的方式配置参数
/* name: name title: title age: age color: color size: size prize: prize */ function doSomeThing(obj){ const _adapter = { name: "obj", title: "test", age: 22, color: "pink", size: 100, prize: 50 } for(i in _adapter){ _adapter[i] = obj[i] || _adapter[i] } console.log(` 姓名:${_adapter.name} 标题:${_adapter.title} 年龄:${_adapter.age} 颜色:${_adapter.color} 尺寸:${_adapter.size} 价格:${_adapter.prize} `) }
测试一下
doSomeThing({ name: "HHH", age: 99, color: "red" })