前端水很深,但我也要试着趟一趟,珍藏了一些易混淆的面试题,避免再次入坑.
js
var和let的使用
`var a = 200;
function test1 () {console.log(a); //undefined var b = a*2; var a = 100; var c = a/2; console.log(a+","+b+","+c); //100,NaN,50
}
test1();``var a = 200;
function test2 () {console.log(a); //200 var b = a*2; a = 100; var c = a/2; console.log(a+","+b+","+c); //100,400,50
}
test2();``var a = 200;
function test3 () {console.log(a); //报错, a is not defined var b = a*2; let a = 100; var c = a/2; console.log(a+","+b+","+c);
}
test3();`
快级作用域和变量提升问题.下面一段代码输出什么
`var b = 10;
function t () {console.log(b);
}
(function(fun) {var b = 20; fun(); //10
})(t);`
`var b = 10;
function t () {console.log(b);
}
(function(fun) {b = 20; fun(); //20
})(t);`
排序算法
快排:`function quickSort (arr) { if (arr.length===0) { return []; } let cIndex = Math.floor(arr.length / 2), c = arr.splice(cIndex, 1)[0], l = [], r = []; for (var i = arr.length - 1; i >= 0; i--) { if (arr[i] < c) { l.push(arr[i]); }else{ r.push(arr[i]); } } return quickSort(l).concat(c,quickSort(r));
}
冒泡:
function bubbleSort (arr) {for (var i = 0; i< arr.length; i++) { for (var j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j+1]) { var temp = arr[j+1]; //元素交换 arr[j+1] = arr[j]; arr[j] = temp; } } } return arr;
}`
//选择
function selectSort (arr) {
var temp,minIndex;
for(var i=0;i<arr.length; i++){minIndex = i; for(var j = i;j<arr.length; j++){ if (arr[j]<arr[minIndex]) { minIndex = j; } } [arr[i],arr[minIndex]] = [arr[minIndex],arr[i]]; // temp = arr[i]; // arr[i] = arr[minIndex]; // arr[minIndex] = temp;
}
return arr;
}
数组生成随机数:
Array.prototype.range = function ( start,end ){
var _self = this;
var length = end - start +1;
var step = start - 1;
return Array.apply(null,{length:length}).map(function (v,i){step++;return step;});
};
- ES6中class的使用
class Person{
get props(){
}return "props";
set props(props){
}console.log('setter: '+props);
constructor(name,age){
}this.name = name; this.age = age;
say(){
}return this.name+","+this.age;
grade(){}
}
class Student extends Person{
constructor(name,age,grade){
}super(name,age); this.grade = grade;
say(){
}return this.name+","+this.age+","+this.grade;
grade1(){
}return this.grade;
}
let p = new Person(‘chao’,23);
var s = new Student(‘c’,22,98);
console.log(p.props);
p.props =”haha”;
console.log(p.props);
console.log(p.say());
console.log(s.grade1());