js–用class类手写简易jquery考虑插件和扩展性
class jQuery {
constructor(selector) {
const result = document.querySelectorAll(selector)
const length = result.length
for (let i = 0; i < length; i++) {
this[i] = result[i]
}
this.length = length
this.selector = selector
}
get(index) {
return this[index]
}
each(fn) {
for (let i = 0; i < this.length; i++) {
const elem = this[i]
fn(elem)
}
}
on(type, fn) {
return this.each(elem => {
elem.addEventListener(type, fn, false)
})
}
}
jQuery.prototype.dialog = function (info) {
alert(info)
}
class myJQuery extends jQuery {
constructor(selector) {
super(selector)
}
addClass(className) {
}
style(data) {
}
}
演示实例如下: