在如今VUE盛行的情況下,我們一直在驚嘆於VUE的組件的功能,卻不知道,原生js早就已經支持了這個功能。
最近在公開課學到的,js還有很多很多需要探索學習。
下面是一個簡單的例子
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>創建標簽</title>
</head>
<body>
<x-product>測試</x-product>
<script type="text/javascript">
var _xproproto=Object.create(HTMLElement.prototype)
_xproproto.createdCallback=function(){
this.addEventListener('click',function(){
alert(1)
})
}
document.registerElement('x-product',{ //注冊標簽,通過原型鏈繼承方法和屬性
prototype:_xproproto
})</script>
</body>
</html>
