原型继承例子

function Elem(id) {
    this.elem = document.getElementById(id);
}
Elem.prototype.html = function(val){
    var elem = this.elem;
    if(val) {
        elem.innerHTML = val;
        return this;
    }else {
        return elem.innerHTML;
    }
}
Elem.prototype.on = function(type,fn){
    var elem = this.elem;
    elem.addEventListener(type,fn);
}

var div1 = new Elem('div');
console.log(div1.html());

div1.html('<p>假装自己是一段文字</p>').on('click', function(){
        alert('clicked')
    }
);

 

posted @ 2019-12-12 16:32  fanmengfei  阅读(164)  评论(0编辑  收藏  举报