class jQuery { constructor(selector) { const result = document.querySelectorAll(selector) console.log(result) const length = result.length for (let i = 0; i < length; i++) { this[i] = result[i] } this.length = length } get(index) { return this[index] } each(fn) { for (let i = 0; i < this.length; i++) { const ele = this[i] fn(ele) } } on(type, fn) { return this.each(ele => { ele.addEventListener(type, fn, false) }) } } //考慮擴展性 //插件 jQuery.prototype.dialog = function (info){ alert(info) } //復寫機制 class MyJQuery extends jQuery{ constructor(selector){ super(selector) } addClass(className){} style(data){} } //使用 const jq = new jQuery('p') console.log(jq.get(0)) jq.each(function(el){ console.log(el) }) jq.on('click',function(el){ console.log(el) })
考點:
- 原型和原型鏈的理解
- dom操作
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/11187.html
標籤:JavaScript
上一篇:前端面試題整理——原型和原型鏈
