外观模式
定义:为多个不同的方法提供一个统一的接口,一般设计到兼容性,对外暴露统一的api,外面只用一个函数,内部自己判断使用具体哪个函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>外观模式</title>
</head>
<body>
<script>
myEvent = {
// 取消冒泡,阻止默认行为
stop(e) {
if (typeof e.stopPropagation === "function") {
e.stopPropagation();
}
if (typeof e.preventDefault === "function") {
e.preventDefault();
}
// IE
if (typeof e.returnValue === "boolean") {
e.returnValue = false;
}
if (typeof e.cancelBubble === "boolean") {
e.cancelBubble = true;
}
},
// 添加事件
addEventListener(dom, type, fn) {
if (dom.addEventListener) {
dom.addEventListener(type, fn, false);
} else if (dom.attachEvent) {
dom.attachEvent("on" + type, fn);
} else {
dom["on" + type] = fn;
}
},
};
</script>
</body>
</html>