前端經(jīng)典手寫(xiě)筆試題

一 請(qǐng)手寫(xiě)拖拽實(shí)現(xiàn)

    // 前提條件 元素需要絕對(duì)定位,設(shè)置left和top屬性
    (function () {
        let oDrag = document.getElementById('drag')
        oDrag.onmousedown = (e) => {
            let disx = e.clientX - oDrag.offsetLeft
            let disy = e.clientY - oDrag.offsetTop
            document.onmousemove = (e) => {
                oDrag.style.left = e.clientX - disx + 'px'
                oDrag.style.top = e.clientY - disy + 'px'
            }
            document.onmouseup = () => {
                document.onmousemove = null
                document.onmousedown = null
            }
        }
    })();

二 請(qǐng)手寫(xiě)es6和es5的面向?qū)ο蟠a實(shí)現(xiàn)

    // 面向?qū)ο?    (function () {
        // es6的面向?qū)ο?        class Person {
            constructor(name, age) {
                this.name = name
                this.age = age
            }
            showName() {
                console.log(this.name)
            }
        }
        // 實(shí)例化Person
        let student = new Person('王宏', '36')
        student.showName()
        // 繼承
        class Worker extends Person {
            constructor(name, age) {
                super(name, age)
            }
            // 添加Worder自己特有的方法
            showAge() {
                console.log(this.age)
            }
        }
        // 實(shí)例化Worker
        let coder = new Worker('廖峰', '40')
        coder.showName()
        coder.showAge()

        // es5的面向?qū)ο?        function Animal(name, color) {
            this.name = name
            this.color = color
        }
        Animal.prototype.showName = function () {
            console.log(this.name)
        }
        // 實(shí)例化Animal對(duì)象
        let dragon = new Animal('龍', '金色')
        dragon.showName()

        // 繼承
        function Tiger(name, color) {
            Animal.call(this, name, color)
        }
        Tiger.prototype = new Animal()
        Tiger.prototype.constructor = Tiger

        let smallTiger = new Tiger('小老虎', '灰色')

        smallTiger.showName()
    })();

三 請(qǐng)手寫(xiě)tab選項(xiàng)卡

    // tab選項(xiàng)卡
    (function () {
        let oBtnBox = document.getElementsByClassName('btn-box')[0]
        let oContentBox = document.getElementsByClassName('content-box')[0]
        let aBtnItem = oBtnBox.getElementsByTagName('button')
        let aContentBox = oContentBox.getElementsByTagName('div')
        let len = aBtnItem.length
        // 其實(shí)用let,那么不需要自定義索引,只用i即可
        for (let i = 0; i < len; i++) {
            aBtnItem[i].idx = i
            aBtnItem[i].onclick = function () {
                for (let j = 0; j < len; j++) {
                    aBtnItem[j].className = ''
                    aContentBox[j].className = 'hide'
                }
                aBtnItem[this['idx']].className = 'active'
                aContentBox[this['idx']].className = ''
            }
        }
    })();

四 請(qǐng)手寫(xiě)ajax的getf方法實(shí)現(xiàn)

    // 手寫(xiě)ajax get
    function get(url, fn) {
        let url = 'api/getlist'
        let xhr = XMLHttpRequest()
        xhr.open('GET', url, true)
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                fn(xhr.responseText)
            }
        }
        xhr.send()
    }

五 請(qǐng)手寫(xiě)ajax的post方法實(shí)現(xiàn)

    // 手寫(xiě)ajax post
    function post(url, data, fn) {
        let url = 'api/getlist'
        let xhr = XMLHttpRequst()
        xhr.open('POST', url, true)
        xhr.setRequestHeader('Content-Type', 'application/json')
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                fn(xhr.responseText)
            }
        }
        xhr.send(data)
    }

六 請(qǐng)手寫(xiě)jsonp的實(shí)現(xiàn)

    // 手寫(xiě)jsonp
    function jsonp(url = '', fn) {
        let fnName = Math.random().toString().slice(2)
        window[fnName] = function (data) {
            fn(data)
        }
        let script = document.creatElement('script')
        script.src = '/api/getlist?cb=' + fnName
        document.body.appendChild(script)
    }

七 請(qǐng)實(shí)現(xiàn)遞歸階乘函數(shù)

    // 手寫(xiě)遞歸 階乘
    function jc(n) {
        if (n === 1) {
            return 1
        }
        return n * jc(n - 1)
    }

八 請(qǐng)實(shí)現(xiàn)深拷貝對(duì)象的函數(shù)實(shí)現(xiàn)(不考慮函數(shù)的拷貝)

    // 深拷貝對(duì)象
    function clone(obj) {
        var newObj = obj instanceof 'Array' ? [] : {} // 這種方法obj不能寫(xiě)實(shí)體對(duì)象,不能直接寫(xiě){a:12}
        // var newObj = Object.protoType.toString.call(obj).slice(8,-1)
        if (typeof obj === 'object' && typeof obj !== 'null') {
            for (let key in obj) {
                if (typeof obj[key] === 'object' && typeof obj[key] !== 'null') {
                    newObj[key] = clone[obj[key]]
                } else {
                    newObj[key] = obj[key]
                }
            }
        } else {
            return obj
        }
        return newObj
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容