阿里巴巴提前批前端编程测评思考

2017-08-13  本文已影响0人  云峰yf

阿里巴巴提前批前端编程测试题

这道题是阿里巴巴2017年7月秋招提前批,前端岗位的编程测评题,原题是给出了html和css代码,让考生写js代码,js代码提示可以用ES6写,并且要用面向对象的思维写。js部分给了一个init()和两句注释。

//第一反应
init()
function init() {
   const deleteBtn = document.querySelectorAll(".user-delete")
   deleteBtn.forEach(d => {
       d.addEventListener("click", removeItem)
   })
}

function removeItem(event) {
   event.target.parentNode.remove()
}
// 事件委托优化
init()
function init() {
    const J_List = document.querySelector("#J_List")
    J_List.addEventListener("click", removeListItem)
}
function removeListItem(e) {
    e.target.className.includes("user-delete") && e.target.parentNode.remove()
}
//OOP思维
class List {
    constructor(contacts="") {
        this.contacts = contacts
    }
    initList(name,delBtnName) {
        const el = document.querySelector(name)
        this.initEvent(el,"click",(e)=>{this.removeItem(e,delBtnName)})
    }
    initEvent(element,eventName,cb){
        element.addEventListener(eventName, cb)
    }
    removeItem(e,delBtnName) {
        e.target.className.includes(delBtnName) && e.target.parentNode.remove()
    }
}
class Contact {
    constructor(id, name = "", sex = "", tel = "", province = "") {
        this.id = id
        this.name = name
        this.sex = sex
        this.tel = tel
        this.province = province
    }
    removeItem() {
        //
    }
}
init()
function init() {
    let list = new List(null)
    list.initList("#J_List","user-delete")
}
//委托对象思维
let DOMPlayer = {
    el:null,
    initDOM(domName) {
        el = document.querySelector(domName)
    },
    initEvent(eventName,cb){
        el.addEventListener(eventName, cb)
    },
    removeItemFromClass(event,itemClassName){
        event.target.className.includes(itemClassName) && event.target.parentNode.remove()
    }
    //更多方法
}

init()
function init() {
    let List = Object.create(DOMPlayer)
    List.initList = function (name) {
        this.initDOM(name)
        this.initEvent("click",(e)=>{
            this.removeItemFromClass(e,"user-delete")
        })
    }
    let list = Object.create(List)
    list.initList("#J_List")
}
上一篇下一篇

猜你喜欢

热点阅读