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('绑定的元素')
// console.log(div1.html())
div1.html('<p>hello imooc</p>')
div1.on('click', function(){
alert('clicked')
})
// 改为链式操作
// 因为上边的两个 return this,相当于把 div1 (对象)给返回回来了
div1.html('<p>hello imooc</p>').on('click', function(){
alert('clicked')
}).html('<p>javascript</p>')
感觉没有用到继承。
js 的原型继承其实并不是面向对象的经典继承方式,java 对于继承的实现,是最经典的继承方式。(需要继续研究)