1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| class jQuery{ constructor (selector) { let result = document.querySelectorAll(selector); let length = result.length; for (let i = 0; i < length; i++) { this[i] = result[i]; } this.length = length; this.selector = selector; }
each(fn) { for (let i = 0; i < this.length; i++) { fn(this[i], i) } }
get(index) { return this[index]; }
on (type, fn) { return this.each((elem) => { window.addEventListener(type, fn, false); }) } }
const $p = new jQuery('p'); $p.each(elem => (console.log(elem))) $p.on('click', elem => (console.log('click:', elem)))
// 插件 jQuery.prototype.dialog = function() { alert('jquery dialog'); }
// 扩展 class myJQuery extends jQuery{ constructor(selector) { super(selector); }
addClass() {
} style() {
} }
|