在如今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>
原生js实现vue组件功能
原文地址:http://www.cnblogs.com/xumqfaith/p/8012833.html