正題
1、
1 var length = 1; 2 function fn() { 3 console.log(this.length); 4 } 5 var obj = { 6 length: 100, 7 action: function (callback) { 8 callback(); 9 arguments[0](); 10 } 11 } 12 obj.action(fn, ...[1, 2, 3, 4]);
2、
1 var a = 10; 2 function test() { 3 console.log(a); 4 a = 100; 5 console.log(this.a); 6 var a; 7 console.log(a); 8 } 9 test();
3、
1 var a = 10; 2 function f1() { 3 var b = 2 * a; 4 var a = 20; 5 var c = a + 1; 6 console.log(b); 7 console.log(c); 8 } 9 f1();
4、
1 var a = 10; 2 function fn1() { 3 console.log(a); 4 } 5 function fn2() { 6 var a = 20; 7 fn1(); 8 } 9 fn2();
5、
1 var a = b = 10; 2 function f1() { 3 var a = b = 20; 4 var c = a + 1; 5 console.log(c); 6 } 7 f1(); 8 console.log(a); 9 console.log(b);
6、
1 const motion = () => { 2 let speed = 100; 3 return { 4 run() { 5 speed++; 6 console.log(speed); 7 } 8 } 9 }; 10 const m1 = motion(); 11 const m2 = motion(); 12 m1.run(); 13 m1.run(); 14 m2.run();
7、
1 var x = 1; 2 function f(y) { 3 return this.x + y; 4 } 5 var obj = { 6 x: 2 7 } 8 var f2 = function () { 9 return f.apply(obj, arguments) 10 } 11 var z = f2(3); 12 console.log(z);
8、
1 setTimeout(() => { 2 console.log(1) 3 }, 100); 4 setTimeout(() => { 5 console.log(2) 6 }, 0); 7 console.log(3); 8 let p = new Promise((resolve, reject) => { 9 resolve(4); 10 console.log(5); 11 }); 12 p.then(res => { 13 console.log(res); 14 }); 15 console.log(6);
9、
1 function f2() { 2 console.log(1); 3 setTimeout(() => { 4 console.log(2); 5 }, 100) 6 } 7 async function f() { 8 console.log(3); 9 await f2(); 10 console.log(4); 11 } 12 f(); 13 console.log(5);
10、
給一個數(shù)組 const arr = [3,1,7,8]和目標(biāo)值n=10,請求出該數(shù)組中元素相加等于目標(biāo)值的下標(biāo)。
比如: arr=[1,2,3,4] n=7; 輸出:[2,3]
解析


1 var length = 1; 2 function fn() { 3 console.log(this.length); 4 } 5 var obj = { 6 length: 100, 7 action: function (callback) { 8 callback(); 9 arguments[0](); 10 } 11 } 12 obj.action(fn, ...[1, 2, 3, 4]); 13 // 考核點:this的指向 14 // 輸出結(jié)果:1 5 15 // 第一個輸出1,是因為fn在全局被調(diào)用,this指向是window 16 // 第二個輸出5,是因為fn被arguments調(diào)用,而arguments里有l(wèi)ength屬性,傳入了五個參數(shù),length為5,所以輸出是5
第2題解析


1 var a = 10; // 全局變量 2 function test() { 3 console.log(a); // 變量提升 4 a = 100; 5 console.log(this.a); // this指向全局的window 6 var a; 7 console.log(a); // 局部變量 8 } 9 test(); 10 // 考核點:變量提升 11 // 輸出結(jié)果:undefined 10 100
第3題解析


1 var a = 10; 2 function f1() { 3 var b = 2 * a; // a=undefined 4 var a = 20; 5 var c = a + 1; 6 console.log(b); 7 console.log(c); 8 } 9 f1(); 10 // 考核點: 11 // 輸出結(jié)果:NaN 21
第4題解析


1 var a = 10; 2 function fn1() { 3 console.log(a); 4 } 5 function fn2() { 6 var a = 20; 7 fn1(); 8 } 9 fn2(); 10 // 考核點: 11 // 輸出結(jié)果:10
第5題解析


1 var a = b = 10; // 相當(dāng)于b = 10; var a = b; 2 function f1() { 3 var a = b = 20; // 相當(dāng)于 b=20; var a = 20; 4 var c = a + 1; // c = 20 + 1 5 console.log(c); 6 } 7 f1(); 8 console.log(a); 9 console.log(b); 10 // 考核點:JavaScript中變量作用域、變量聲明提升和變量賦值的知識點 11 // 輸出結(jié)果:21 10 20
第6題解析


1 const motion = () => { 2 let speed = 100; 3 return { 4 run() { 5 speed++; 6 console.log(speed); 7 } 8 } 9 }; 10 const m1 = motion(); 11 const m2 = motion(); 12 m1.run(); 13 m1.run(); 14 m2.run(); 15 // 考核點:閉包 16 // 輸出結(jié)果:101 102 101
舉例:


1 var a = 100; 2 function f1() { 3 a++; 4 console.log(a); 5 } 6 f1(); // 101 7 f1(); // 102 8 9 function f1() { 10 var a = 100; 11 a++; 12 console.log(a); 13 } 14 f1(); // 101 15 f1(); // 101 16 17 // 閉包保存變量的狀態(tài) 18 function f1() { 19 var a = 100; // 局部變量 20 return function () { 21 a++; 22 console.log(a); 23 } 24 } 25 26 var a1 = f1(); 27 a1(); // 101 28 a1(); // 102
第7題解析


1 var x = 1; 2 function f(y) { // 函數(shù)申明 3 return this.x + y; // this指向 4 } 5 var obj = { 6 x: 2 7 } 8 var f2 = function () { // 函數(shù)表達(dá)式 9 return f.apply(obj, arguments) // obj={ x: 2 }, arguments=3,arguments 類似于數(shù)組,代表參數(shù)集合 10 } 11 var z = f2(3); 12 console.log(z); 13 // 考核點:call()和apply(),改變this的指向 arguments 14 // 輸出結(jié)果:5
舉例:


1 f.apply(obj, 參數(shù)); 2 3 var id = 10; 4 function fn() { 5 console.log(this.id); // 10 6 } 7 fn(); 8 9 var o1 = { 10 id: 999 11 } 12 // 如何讓fn函數(shù)中this的指向o1 13 fn.apply(o1) // this.id = o1.id 相當(dāng)于fn函數(shù)在o1對象中執(zhí)行 14 15 function Person(name, age) { 16 this.name = name; 17 this.age = age; 18 } 19 20 var y1 = new Person('y1', 20); 21 var o2 = {}; //o2空對象 22 Person.apply(o2, ['o2', 18]); // apply 參數(shù)是一個數(shù)組 23 console.log(o2.name); 24 25 var o3 = {}; 26 Person.call(o3, "hsl", 18); // call 參數(shù)是字符串 27 console.log(o3.name);
第8題解析


1 setTimeout(() => { 2 console.log(1) 3 }, 100); // 異步-宏任務(wù) 4 setTimeout(() => { 5 console.log(2) 6 }, 0); // 異步-宏任務(wù) 7 console.log(3); // 同步 8 let p = new Promise((resolve, reject) => { 9 resolve(4); 10 console.log(5); // 同步 11 }); 12 p.then(res => { 13 console.log(res); // 異步-微任務(wù) 14 }); 15 console.log(6); // 同步 16 // 考核點:同步、異步-微任務(wù)、異步-宏任務(wù) 17 // 微任務(wù):Promise的then『Promise同步,但Promise中的then是異步』、async await 18 // 宏任務(wù):setTimeout、setInterval 19 // 輸出結(jié)果:3 5 6 4 2 1
第9題解析


1 function f2() { 2 console.log(1); 3 setTimeout(() => { 4 console.log(2); 5 }, 100) 6 } 7 async function f() { 8 console.log(3); 9 await f2(); 10 console.log(4); 11 } 12 f(); 13 console.log(5); 14 // 考核點:async和await 15 // 輸出結(jié)果:3 1 5 4 2
第10題解析文章來源:http://www.zghlxwxcb.cn/news/detail-710094.html


1 /* 2 給一個數(shù)組 const arr = [3,1,7,8]和目標(biāo)值n=10,請求出該數(shù)組中元素相加等于目標(biāo)值的下標(biāo)。 3 比如: arr=[1,2,3,4] n=7; 輸出:[2,3] 4 */ 5 6 // 方法一 7 function arrFn(arr, n) { 8 let newArr = []; 9 for (let i = 0, len = arr.length; i < len; i++) { 10 for (let j = i + 1, len = arr.length; j < len; j++) { 11 if (n === arr[i] + arr[j]) { 12 newArr.push(i, j); 13 } 14 } 15 } 16 return newArr; 17 } 18 console.log(arrFn([3, 1, 7, 8], 10)); 19 20 // 方法二 21 function twoSum(nums, target) { 22 // 1、構(gòu)造哈希表 23 const map = new Map(); // 存儲方式 {need, index} 24 25 // 2、遍歷數(shù)組 26 for (let i = 0; i < nums.length; i++) { 27 // 2.1 如果找到 target - nums[i] 的值 28 if (map.has(nums[i])) { 29 return [map.get(nums[i]), i] 30 } else { 31 // 2.2 如果沒找到則進(jìn)行設(shè)置 32 map.set(target - nums[i], i) 33 } 34 } 35 } 36 console.log(twoSum([3, 1, 7, 8], 10));
?鑒定完畢,歡迎友友們一起交流學(xué)習(xí)??!文章來源地址http://www.zghlxwxcb.cn/news/detail-710094.html
到了這里,關(guān)于前端高頻面試題匯總正題+(附答案解析)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!