• 2017阿里巴巴前端在线笔试题


    选择题:

    1、

    let obj = Object.create({name: 'King'});
            function foo(obj){
                Object.setPrototypeOf(obj, null);
                return obj;
            }
            console.log(obj === foo(obj)); // true
    
            let obj = [1,2,3];
            function foo(val){
                val = [1,2,3];
                return val;
            }
            console.log(obj === foo(obj)); // false
    
            let obj = 1;
            function foo(val){
                val += 1;
                return val; 
            }
            console.log(obj === foo(obj)); // false
    
            let obj = {bar: 1};
            function foo(val){
                val.bar += 1;
                return val
            }
            console.log(obj === foo(obj)); // true
    
            let obj = [1,2,3];
            function foo(val){
                const newVal = val.map(num => num * 2);
                return newVal;
            }
            console.log(obj === foo(obj)); // false
    
            let obj = [1,2,3];
            function foo(val){
                return val.sort();
            }
            console.log(obj === foo(obj)); // true

    主要考察引用对象的引用地址是否相同

    2、

    <body>
        <div id="J_container">
            <div id="div2">
                <div id="div1">点我</div>        
            </div>
        </div>
            <script>
            var div1 = document.getElementById('div1');
            var div2 = document.getElementById('div2');
            div1.addEventListener('click', () => {console.log('a')}, true);
            div2.addEventListener('click', () => {console.log('b')});
            div1.addEventListener('click', () => {console.log('c')}, false);
            div2.addEventListener('click', () => {console.log('d')}, true);
        </script>
    </body>    

    输出的顺序是: d a c b

    第一道编程题:

    要求:

    我的解答:

    function cycleDetector(obj){
                let stack = [];
                let res = false;
                let _incircle = function(item){
                    let len = stack.length;
                    for( let y = 0; y < len; y++){
                        if( item === stack[y]){
                            return true;
                        }
                    }
                    return false;
                }
                let _read = function(_obj){
                    for ( let pro in _obj ){
                        if( _obj[pro] instanceof Object ){
                            if ( !_incircle(_obj[pro]) ){
                                stack.push(_obj[pro]);
                                _read(_obj[pro]);
                            }else{
                                res = true;
                            }
                        }
                    }
                };
                _read(obj);
                return res;
            }

    第二道编程题:

    要求:

    我的解答:

         class EventEmitter {
                /* 在此处填写实现 */
                constructor(){
                    this.eventlist = {};
                    this.onceEventlist = {};
                }
                fire(obj){
                    const { type, value } = obj;
                    let _run = 1;
                    let _find = function(list){
                        for(let i in list){
                            if(i === type){
                                for(let j in list[i]){
                                    list[i][j](value);
                                    if( _run ){
                                        delete list[i];
                                    }
                                }
                            }
                        }
                        _run = 0;
                    };
                    _find(this.onceEventlist);
                    _find(this.eventlist);
                }
                on(event, fn){
                    this.eventlist[event] = {};
                    this.eventlist[event][fn.name] = fn;
                }
                off(event, fn){
                    if(this.eventlist[event]){
                        delete this.eventlist[event][fn.name];
                    }
                }
                once(event, fn){
                    this.onceEventlist[event] = {};
                    this.onceEventlist[event][fn.name] = fn;
                }
            }
    
            const emitter = new EventEmitter();
    
            const handler = function(evt) {
                console.log(1, evt);
            };
            emitter.on('foo', handler);
    
            emitter.once('foo', function(evt) {
                console.log(2, evt);
            });
    
            emitter.fire({ type: 'foo', value: 'hello' });
            emitter.fire({ type: 'foo', value: 'world' });
            emitter.off('foo', handler);
            emitter.fire({ type: 'foo', value: 'test' });

    三、其它

    一道模板解析习题

    想法就是通过正则解析:

    var template = function(str) {
        var replace = {};
        var result;
        var newStr = str;
        var re = new RegExp('\<\%\=\w+\%\>','g');
        while((result = re.exec(str)) !== null){
            var _re = new RegExp('\w+','g');
            var item = result[0].match(_re)[0];
            replace[item] = {};
            replace[item].origin = result[0];
        }
        return function(obj) {
            for(var i in obj){
                if(replace[i]){
                    str = str.replace(replace[i].origin, obj[i]);
                }
            }
            return str;
        }
    }
  • 相关阅读:
    消息摘要算法示例(python和go)
    试设计代码生成器模式[初步]
    Webb.WAVE.Controls.Upload2已经完成,正在测试中。
    QuickGuide for AJAX[简译AJAX快速指南]以及对现有WebService的扩展。
    WebbUpload2测试版--HTTP协议下,用IE上传大文件解决方案,[附源码]
    [转]在APACHE上运行asp.net
    一段比较经典的多线程学习代码
    Catch the star that will take you to your dream摘取命运的启明星
    ASP.net页面上的默认Submit按钮!
    [转]Ajax在Sun上的理论
  • 原文地址:https://www.cnblogs.com/lastnigtic/p/7420080.html
Copyright © 2020-2023  润新知