1、typeof
typeof NaN//'number'
typeof null//'object'
typeof 'undefined' //undefined
2、对象深拷贝
写一个函数复制对象
普通对象可以
let newObj = Object.assign({},obj)或者for in遍历对象,当对象里有数组、对象的时候可用下面的方法
function clone(obj){//当对象里有函数 null、undefined时会忽略
return JSON.parse(JSON.stringify(obj))
}
function clone(obj){//保险做法,遍历每一项
if(!obj && typeof obj !== 'object'){
return;
}
var newObj = {};
for(var key in obj){
o[key] = (obj[key] && typeof obj[key] === 'object')?clone(obj[key]):obj[key] ;
}
return newObj;
}
3、写出一个两个数组里面的交集和差集
来自尤雨溪知乎答案;点击打开链接
let intersection = a.filter(v => b.includes(v));
let difference = a.concat(b).filter(v => !a.includes(v) || !b.includes(v))
4、写出一个字符串出现次数最多的次数和字符
console.log(new CharMax('asdfgasdkjfklsjklj'))
function CharMax(str){
this.obj = {};
this.len = str.length;
this.max = 0;
this.maxChar = '';
for(var i = 0; i < this.len; i++){
var c = str.charAt(i);
if(this.obj[c]){
this.obj[c]++;
}else{
this.obj[c] = 1;
}
}
console.log(this.obj)
for(var key in this.obj){
if(this.max < this.obj[key]){
this.max = this.obj[key];
this.maxChar = key;
}
}
console.log(this.maxChar + ":" + this.max);
}
//123456789翻转成RMB形式然后每三个用','分割123,456,789
let o = {};
o.n = 123456789;
o.num = o.n.toString();
o.a = o.num.split('').reverse();
o.arr = [];
for(let i = 0,len = o.a.length; i < len; i++){
o.arr.push(o.a[i]);
if((i+1) % 3 == 0 && len - i > 1){
o.arr.push(',')
}
}
console.log(o.arr.reverse().join(''))
//生成5个不同是随机数
let arr = [];
function random(){
let num;
for(var i = 0; i < 5; i++){
arr[i] = Math.floor(Math.random()*10 + 0);
for(var j = 0; j < i; j++){
if(arr[i] == arr[j]){
i--;//注意这一点
}
}
}
return arr
}
console.log(random())
//去除数组中重复的数字
let obj = {};
obj.arr1 = [2,2,3,4,5,6];
obj.flag = 1;
function remove(arr){
let newArr = [];
for(let i = 0; i < arr.length; i++,obj.flag = 1){
for(let j = 0; j < i; j++){
if(arr[i] == newArr[j]){
obj.flag = 0;
console.log(0)
}
}
obj.flag ? newArr.push(arr[i]) : 0
}
return newArr;
}
console.log(remove(obj.arr1))
//第二种方法
function removeRepeat(arr){
let newArr = [];
for(let i = 0,len = arr.length; i < len; i++){
if(newArr.indexOf(arr[i]) == -1){
newArr.push(arr[i])
}
}
return newArr;
}
console.log(removeRepeat(obj.arr1))
5、清除浮动
.clearfix:after{
display:block;
content: '';
height: 0;
visibility: hidden;
font-size:0;
clear: both;
}
.clearfix{
*display:inline-block;
_height: 1%;
}
6、垂直居中
body {
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
display: -webkit-flex;
display: flex;
}
ie11有bug
点击打开链接
7、
var name = 'world';
(function(){//自执行函数,先访问内部同名函数,此时变量name提升
if(typeof name === 'undefined'){
var name = 'jack';
console.log('goodbye' + name);
}else{
console.log('hello' + name)
}
})()//goodbyejack
8、常见基础
https://github.com/qiu-deqing/FE-interview点击打开链接
- IE5-8不支持
opacity
,解决办法:
.opacity {
opacity: 0.4
filter: alpha(opacity=60); /* for IE5-7 */
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=60)"; /* for IE 8*/
}
9、
function Foo(){
getName = function(){alert(1)};
return this;
}
Foo.getName = function(){alert(2)};
Foo.prototype.getName = function(){alert(3)}
var getName = function(){alert(4)};
function getName(){alert(5)};
Foo.getName()//2
getName()//4
Foo().getName()//1
getName()//1
new Foo.getName()//2
new Foo().getName()//3
new new Foo().getName()//3