码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-08-25 01:18:28      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:log   png   选择   run   pre   blog   else   creat   nbsp   

选择题:

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‘ });

 

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

标签:log   png   选择   run   pre   blog   else   creat   nbsp   

原文地址:http://www.cnblogs.com/lastnigtic/p/7420080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!