很多变态的好玩的前端面试题

1. javascript的 “;”导致的一个问题

function foo1()
{ 

  return { 

    bar: "hello" 

  };

} 

function foo2()

{ 

  return 

    { 

      bar: "hello" 

    };

}

const a = foo1();

const b = foo2();

console.log(a) //Object {bar: "hello"}

console.log(b) //underfind

2. 阿里面试题 ---- 分别用es6 和 es3 方式输出 1 2 3 done [1, 2, 3]      

const timeout = ms => new Promise((resolve, reject) => {

           setTimeout(() => {

              resolve();

           }, ms)

       })

       const ajax1 = () => timeout(2000).then(() => {

           console.log('1');

           return 1;

       })

       const ajax2 = () => timeout(1000).then(() => {

           console.log('2');

           return 2;

       })

       const ajax3 = () => timeout(2000).then(() => {

           console.log('3');

           return 3;

       })

       const mergePromise = ajaxArray => {

           // 在此处分别用es6 和 es3 方式输出 1 2 3 done [1, 2, 3]

       }



       mergePromise([ajax1, ajax2, ajax3]).then(data => {

           console.log('done');

           console.log(data);

       });



// es3

       const mergePromise = ajaxArray => {

           var arr = [],

              ajaxLen = ajaxArray.length;

           for (var i = 0; i< ajaxLen; i ++) {

              ajaxArray[i].next = ajaxArray[i + 1];

           }

           function todo (item) {

              item().then(data => {

                  arr.push(data);

                  var _next = item.next;

                  _next && todo(_next);

              })

           }



           todo.then = data => {

              ajaxArray[ajaxLen - 1].next = () =>

                  timeout(0).then(() => {

                     data(arr);

                  })

             

              todo(ajaxArray[0]);

           }

           return todo;

       }

// es6

       const mergePromise = ajaxArray => {

           let result = [];

           async function asyncForEach () {

              for (let item of ajaxArray) {

                  result.push(await item());

              }

              return result;

           }



           return asyncForEach();

       }

 

3. 在什么情况下“a == 2 && a == 3”的返回值是true

let i = 2;
Number.prototype.valueOf = function() {
    return i++;
};
let a = new Number( 42 );
if (a == 2 && a == 3) {
    console.log( true );
}

 

发布了19 篇原创文章 · 获赞 3 · 访问量 151万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览