判断以下程序的输出结果

  1. setTimeout(()=>{console.log(1);},20);

    setTimeout(()=>{console.log(2);},0);

    console.time("while");

    let i=0;

    while (i<=99999999) {

        i++;

    }

    console.timeEnd('while')

    setTimeout(()=>{console.log(3);},10);

    console.log(4);

    答案:while:89.590ms 4 2 3 1

  2. for循环遍历 利用splice去重
    function unique(arr){
        for(var i=0;i<arr.length;i++){
            for(var j=i+1;j<arr.length;j++){
                if(arr[i]==arr[j]){ 
                //如果第一个等于第二个,splice方法删除第二个
                arr.splice(j,1);
                j--;
                }
            }
        }
        return arr;
    }
     
    let arr = [1,1,1,2,2,3,3];
    console.log(unique(arr));

    答案:1,2,3

  3. for(var i=0;i<5;i++){

        setTimeout(function(){

            console.log(i);

        },0);

    }

    console.log(i);
    答案:5 5 5 5 5
    函数定义时 函数内容是不执行的 所以i还是i 不会变成0,1,2,3,4 定时器中的回调函数只能在主程序执行完才能开始执行,当主程序执行完 循环变量i已经被改为5了

  4. window.color="red";
    let color="green";
    let obj={
        color:'blue'
    };
    let sayColor=()=>{
        return this.color;
    };
    console.log(sayColor.apply(obj));
    let a=10;
    console.log(window.a);
    答案:red undefined
    let相当于匿名函数自调,所以 let声明的变量 不会自动加入到window
    箭头函数内外this通用 所以apply也无法替换sayColor函数内的this
    所以this指向window 所以输出red

  5. function change(){

        alert(typeof fn)

        function fn(){

        alert('hello')

        }

        var fn

    }

    change();
    答案:function
    function fn(){...}被整体声明提前了
    var fn 发现已经有fn变量了 就不在重复创建,所以 var fn没作用

  6. a=3

    a.prop=4;

    alert(a+a.prop)

    答案:NaN
    a.prop=4,等效于new Number(a).prop=4,但是new Number(a),使用后自动释放 4也就不存在了 再次使用a.prop,又等效于new Number(a),所以没有prop属性,值为undefined,数字+undefined,undefined隐式转换为数字NaN,导致计算结果为NaN

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值