-
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 -
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 -
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了 -
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 -
function change(){
alert(typeof fn)
function fn(){
alert('hello')
}
var fn
}
change();
答案:function
function fn(){...}被整体声明提前了
var fn 发现已经有fn变量了 就不在重复创建,所以 var fn没作用 -
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
判断以下程序的输出结果
最新推荐文章于 2023-02-24 21:24:42 发布