总结一些前端基础的知识,有些知识可能在前端面试的时候会问到,因此作个记录,也有助于其余人查看,若是有什么问题,能够指出,会积极修正。javascript
console.log(typeof undefined); //undefined
console.log(typeof 123); //number
console.log(typeof '123'); //string
console.log(typeof true); //boolean
console.log(typeof [1,2,3]); //object
console.log(typeof {"id": 11}); //object
console.log(typeof null); //object
console.log(typeof console.log); //function
复制代码
==
,什么时候使用===
除了obj.a == null
之外,都用===
,==
要用的时候必定要是已经定义的
obj.a == null
转换以后实际上是 obj.a == null || obj.a == undefined
html
Object
Array
Boolean
Number
String
Function
Date
RegExp
Error
复制代码
区别:值类型改变一个不会影响其余的,引用类型改变都改变,由于公用内存块前端
JSON是JS的一个对象,也是一种数据格式,JSON中的两个api以下java
JSON.parse()
JSON.stringify()
Object.prototype.toString
获取一个对象的类型var toString = Object.prototype.toString;
toString.call(new Date); // [object Date]
toString.call(new String); // [object String]
toString.call(Math); // [object Math]
toString.call(/s/); // [object RegExp]
toString.call([]); // [object Array]
//Since JavaScript 1.8.5
toString.call(undefined); // [object Undefined]
toString.call(null); // [object Null]
复制代码
arr instanceof Array
面试
instanceof判断一个引用类型是什么引用类型,是经过__proto__(隐式原型一层一层往上找,可否找到对应构造函数的prototype)ajax
function Element(ele) {
this.ele = document.getElementById(ele);
}
Element.prototype.html = function(val) {
var ele = this.ele;
if (val) {
ele.innerHTML = val;
return this;
} else {
return ele.innerHTML;
}
};
Element.prototype.on = function(type, fn) {
var ele = this.ele;
ele.addEventListener(type, fn);
return this;
}
var element = new Element('main');
element.html('hello').on('click', function() {
alert('handleClick');
});
复制代码
function Foo(name) {
this.name = name;
// return this; // 自己会执行这一步
}
var f = new Foo('shiyanping');
复制代码
如下两种状况会进行提高:api
var a = {
name: 'A',
fun: function() {
console.log(this.name);
}
};
a.fun(); //this === a
a.fun.call({ name: 'B' }); //this === { name: 'B' }
var fun1 = a.fun;
fun1(); //this === window
复制代码
var i;
for (i = 0; i < 10; i++) {
(function(i) {
// 函数做用域
var a = document.createElement('a');
a.innerHTML = i + '<br>';
a.addEventListener('click', function(e) {
e.preventDefault();
alert(i);
});
document.body.appendChild(a);
})(i);
}
复制代码
// 判断一个数字是否出现过
function isFirst() {
var _list = [];
return function(id) {
if (_list.indexOf(id) >= 0) {
return false;
} else {
_list.push(id);
return true;
}
};
}
var first = isFirst();
first(10);
first(10);
first(20);
复制代码
同步会阻塞代码,可是异步不会 alert是同步 setTimeout是异步数组
console.log(1);
setTimeout(function() {
console.log(2);
}, 0);
console.log(3);
setTimeout(function() {
console.log(4);
}, 1000);
console.log(5);
// 输出结果:1,3,5,2,4
复制代码
须要等待的状况下都须要异步,由于不会像同步同样阻塞网络
console.log(Date.now()); // 获取当前毫秒数
var dt = new Date(); // 获取当前时间
console.log(dt.getTime()); // 当前时间的毫秒数
console.log(dt.getFullYear()); // 年
console.log(dt.getMonth()+1); // 月(0-11)
console.log(dt.getDate()); // 日(0-31)
console.log(dt.getHours()); // 时(0-23)
console.log(dt.getMinutes()); // 分(0-59)
console.log(dt.getSeconds()); // 秒(0-59)
复制代码
Math.random()
- 返回 0 ~ 1 之间的随机数Math.abs(x)
- 返回数的绝对值Math.ceil(x)
- 向上取整Math.floor(x)
- 向下取整var arr = ['a', 'b', 'c', 'd'];
arr.forEach(function (item, index) {
console.log(item + ',' + index);
})
复制代码
// map,生成新数组,不改变原来数组的格式
var arr = ['a', 'b', 'c', 'd'];
var result = arr.map(function (item, index) {
return index + '/' + item;
})
console.log(result);
复制代码
// sort, 会改变原来数组
var arr = [1, 23, 3, 4];
var result = arr.sort(function (a, b) {
// 从小到大排序
return a - b;
// 从大到小排序
// return b - a;
})
console.log(result);
复制代码
var arr = [1, 2, 3, 4];
var result = arr.filter(function (item, index) {
if (item < 3) {
return true
}
})
console.log(result);
复制代码
var arr = [1, 2, 3, 4];
var result = arr.every(function (item, index) {
if (item < 3) {
return true
}
})
console.log(result); // false
复制代码
var arr = [1, 2, 3, 4];
var result = arr.some(function (item, index) {
if (item < 3) {
return true
}
})
console.log(result); // true
复制代码
var arr = [1, 2, 3, 4];
var result = arr.join(',');
console.log(result);
复制代码
var arr = [1, 2, 3, 4];
var result = arr.reverse();
console.log(result);
复制代码
var obj = {
x: 10,
y: 20,
z: 30
}
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(key + ':' + obj[key]);
}
}
复制代码
function formatDate(dt) {
if (!dt) {
dt = new Date();
}
var year = dt.getFullYear();
var month = dt.getMonth() + 1;
var date = dt.getDate();
if (month < 10) {
month = '0' + month;
}
if (date < 10) {
date = '0' + date;
}
return year + '-' + month + '-' + date;
}
var nowDate = new Date();
var formatDate = formatDate(nowDate);
console.log(formatDate);
复制代码
function randomStr(len) {
var random = Math.random();
random = random + '0000000000'; // 防止自动生成的数字不知足长度报错而且强制转换成字符串
return random.substr(0, len)
}
console.log(randomStr(20));
复制代码
function forEach(obj, fn) {
if (obj instanceof Array) {
obj.forEach(function (item, index) {
fn(index, item);
})
} else {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
fn(key, obj[key]);
}
}
}
}
var arr = [1, 2, 3, 4];
forEach(arr, function (index, item) {
console.log(index + ',' + item);
});
var obj = {
x: 10,
y: 20
};
forEach(obj, function (index, item) {
console.log(index + ',' + item);
});
复制代码