老生常谈JS中的继承及实现代码(2)

class Drag{ constructor(id){ this.ele=document.getElementById(id); this.init(); }; init(){ var that=this; this.ele.onmousedown=function(e){ var e=event||window.event; that.disX=e.offsetX; that.disY=e.offsetY; document.onmousemove=function(e){ var e=event||window.event; that.move(e); } that.ele.onmouseup=function(){ document.onmousemove=null; that.ele.onmouseup=null; } } }; move(e){ this.ele.style.left=e.clientX-this.disX+"px"; this.ele.style.top=e.clientY-this.disY+"px"; } } new Drag("drag1"); class ExtendsDrag extends Drag{ constructor(id){ super(id); } } new ExtendsDrag("drag2")

我总结的这几种继承方法.两个demo继承的方法大家最好在编译器上跑一下,看看。这样才能更深刻的去理解。尤其是原型链的继承,js作为一个面向对象的编程语言,还是很常用的。

总结

以上所述是小编给大家介绍的JS中的继承及实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/a8e62a472822735d2d8f4364e20a586c.html