下面案例,创建一个函数createElement,利用解构实现创建元素的默认值配置。
function createElement(optfons = {}) {
let { width = 200,height = 100, backgroundColor =”red" } = optfons;
const div = document. createElement("div");
div . style.width = width +"px";
div . style. height = height +px" ;
div . style. backgroundColor = backgroundColor ;
document . body . appendChild(div);
}
createElement({ width: 60,height: 30,backgroundColor:green})
|