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)//用于向指定元素添加时间句柄
return this;
}
var div1 = new Elem('div1')//把对应id写在这里
// console.log(div1.html())
div1.html('<p>hello World</p>').on('click',function(){//链式操作
alert('clicked')
}).html('<p>javascript</P>')
来自:慕课网《前端JavaScript面试技巧》2-11原型链继承例子