一、JS基础
01、数据类型
1、数据类型的分类
五种基本数据类型: number、string、boolean、undefined、null。
一种引用数据类型: object
2、数据类型检测
方法1: typeof 变量
方法2: 变量 instanceof Array
3、数据类型转换
// 隐式转换
(1)转number: let n = +a
(2)转string: let n = a + ''
(3)转Boolean:let n = !a // 转Boolean时,只有false、null、undefined、0、NaN、''的情况下为false。
(4)短路运算: 最终返回的结果是判定后的结果,而不是boolean值。
// 强制转换
4、包装类
JS为boolean、string、number单独创建了一个构造函数,将它们包装成一个对象。
5、面试题
(1)运算结果考题
console.log( true + 1 ); //2
console.log( 'name'+true ); //nameture
console.log( undefined + 1 ); //NaN
console.log( typeof undefined );//undefined
console.log( typeof(NaN) ); //number
console.log( typeof(null) ); //object
(2)typeof、instanceof有什么区别?
typeof: 用于判断数据类型,返回一个字符串,适用于原始数据类型。
instanceof: 用于判断对象是否是某个类的实例,适用于引用类型的判断。
(3)如何实现自己的instanceof()方法?
function instanceofs(target, obj) {
let p = target
while(p) {
if(p === obj.prototype) {
return true
} else {
p = p.__proto__
}
}
return false
}
02、对象
1、标准库
2、如何创建对象(JS中所有的对象都是由构造函数产生。 )
// 1、原始写法(构造函数)
function Person(arg1, arg2) {
this.name = arg1
this.age = arg2
}
const obj = new Person()
// 2、语法糖1
const obj = {
'name': '张三',
'age':18,
'1': '思密达',
'2': '小甜甜',
'how are you': 'fine thank you'
}
// 3、语法糖2
// 对象键值对写法:所有属性名都是字符串,但属性名是纯数字、单词时可以省略引号。
const obj = {
name: '张三',
age: 18,
1: '思密达',
2: '小甜甜',
'how are you': 'fine thank you'
}
语法糖原理:
const obj = {
name: '张三',
age: 14
}
// 实际上是JS底层是这样做的
const obj = new Object()
obj.name = '张三'
obj.age = 14
3、对象的读法
// 对象.属性名,其实是取巧方法,真正的读法是:对象['属性名']
const obj = {
name: '张三',
age: 18,
1: '思密达',
2: '小甜甜',
'how are you': 'fine thank you'
}
obj['name'] //原始写法
obj[0] === obj['0'] //原始写法
obj.name //语法糖
4、对象的遍历
const obj = {
name: 'snake',
age: 141,
sport: football
}
// 情况1:默认会遍历对象的全部属性,包括隐式原型上面的属性。
for(let key in obj) {
console.log(obj[key])
}
// 情况2:如果遍历时只想遍历对象自身的属性,不想遍历到隐式原型上面的属性,这样做:
for(let key in obj) {
if(obj.hasOwnProperty(key)) {
console.log(obj[key])
}
}
03、数组
1、数组标准类(Array)
push(i) 【尾增】向数组末尾添加一个或多个元素,并返回新的长度。
pop(i) 【尾删】删除数组末尾的元素,并返回被删除的元素。
shift(i)【头删】删除数组头部的元素,并返回被删除的元素。
unshift(i) 【头增】向数组头部添加一个或多个元素,并返回新的长度。
concat(arr1, arr2) 【合并数组】连接两个或多个数组,返回一个新数组。
slice(start, end) 【截取数组】返回数组的指定部分,不会改变原数组。
splice(index) 【删除指定部分】删除index之后的全部元素。(修改原数组)
splice(index, n) 【删除指定部分】从index开始,删除n个元素。(修改原数组)
splice(i1, i2, i3)【删除并插入】删除i1~i2之间的元素,并插入i3。(修改原数组)
splice(index, n, i1, i2...)【删除并插入】 从index开始,删除n个,并插入i1、i2....
forEach() 【遍历】无返回值遍历。
map() 【映射】把每个元素进行映射,返回新数组。
filter() 【过滤】过滤出符合条件的,返回新数组。
find() 【找元素】返回数组中第一个满足条件的元素。
findIndex() 【找元素】返回数组中第一个满足条件的元素的索引。
reduce() 【累加器】对数组中的每个元素执行一个累加器函数,将其结果汇总为单个返回值。
some() 【找元素】只要找到一个符合的,就是true。
every() 【找元素】必须全部都符合条件,才是true。
2、数组的写法
// 伪数组写法(数组的本质是对象,这个写法产生的是伪数组)
const obj = {
'0': 24,
'1': 48,
'2': 41,
'3': 278
}
// 数组写法
const obj = new Array(24, 48, 41, 278)
// 语法糖
const obj = [24, 48, 41, 278]
语法糖的原理:
const arr = [1, 2, 3, 4]
//实际上JS底层是这样做的
const arr = new Array(1,2,3,4)
3、伪数组 => 真数组
// 伪数组的特点
(1) 具备属性length,记录伪数组的元素数量。
(2) 可以使用for循环遍历。
(3) 不具备真数组的方法,如push、pop等。
// 伪数组转成真数组
const arr = Array.from( arguments )
const arr = Array.from(new Set([1,2,1,2,3]))
const arr = [ ...arguments ]
const arr = Array.prototype.slice.call(伪数组)
4、数组去重
// 方法1:用set集合
const array = [1, 2, 2, 3, 4, 4, 5]
const uniqueArray = [...new Set(array)]
// 方法2:使用 Array.filter() + indexOf()
const array = [1, 2, 2, 3, 4, 4, 5]
const uniqueArray = array.filter((value, index, self) =>
self.indexOf(value) === index)
// 方法3:使用 Array.reduce() + includes()
const array = [1, 2, 2, 3, 4, 4, 5]
const uniqueArray = array.reduce((acc, currentValue) =>
acc.includes(currentValue) ? acc : [...acc, currentValue], [])
// 方法4:循环校验
const array = [1, 2, 2, 3, 4, 4, 5];
const uniqueArray = [];
for (let i = 0; i < array.length; i++) {
if (uniqueArray.indexOf(array[i]) === -1) {
uniqueArray.push(array[i]);
}
}
5、数组去扁平化
const nestedArray = [1, 2, [3, 4], [ 5, 6, [7, 8] ], 9, 9, 2]
// 扁平化
const flattenedArray = nestedArray.flat(Infinity);
// 去重
const uniqueArray = [...new Set(flattenedArray)];
6、在数组中,找出重复次数最多的元素
function findMostFrequentElement(arr) {
let countMap = {};
let maxCount = 0;
let mostFrequentElement = null;
arr.forEach((element) => {
countMap[element] = (countMap[element] || 0) + 1;
if (countMap[element] > maxCount) {
maxCount = countMap[element];
mostFrequentElement = element;
}
});
return mostFrequentElement;
}
// 示例数组
const array = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4];
const mostFrequentElement = findMostFrequentElement(array);
console.log(`重复次数最多的元素是: ${mostFrequentElement}`);
7、数组 => 字符串
// 方法1:join()方法
const arr = [1, 2, 3, 4, 5];
const str = arr.join(); // "1,2,3,4,5"
const strWithDash = arr.join('-'); // "1-2-3-4-5"
// 方法2:toString()方法
const arr = [1, 2, 3, 4, 5];
const str = arr.toString(); // "1,2,3,4,5"
// 方法3:转JSON
const arr = [1, 2, 3, 4, 5];
const str = JSON.stringify(arr); // "[1,2,3,4,5]"
8、js数组长度为10,删除比如第五个怎么办 ?
// 方法1
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
arr.splice(4, 1) // 删除第五个元素(索引为4)
console.log(arr) // [1, 2, 3, 4, 6, 7, 8, 9, 10]
// 方法2
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let newArr = arr.slice(0, 4).concat(arr.slice(5))
console.log(arr); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(newArr); // [1, 2, 3, 4, 6, 7, 8, 9, 10]
9、判断是否数组 ?
方式一:Array.isArray()
const arr = [1, 2, 3];
console.log(Array.isArray(arr));
方式二:isPrototypeOf():用于判断当前对象是否为另外一个对象的原型,
如果是就返回 true,否则就返回 false。
var arr = [1,2,3];
console.log( Array.prototype.isPrototypeOf(arr) )
方式三:constructor
var arr = [1,2,3];
console.log( arr.constructor.toString().indexOf('Array') > -1 )
方式四:Object.prototype.toString.call()
const arr = [1, 2, 3];
console.log(Object.prototype.toString.call(arr) === '[object Array]');
方式五:instanceof
const arr = [1, 2, 3];
console.log(arr instanceof Array);
04、字符串
1、标准库
// 1、根据编码值num,返回所对应的字符。
String.formCharCode(num)
例如:String.formCharCode(97),得到结果是 'a'
// 2、获取当前字符串的长度
String.prototype.length
// 3、根据当前字符串,得到某个下标num所对应的编码。
String.prototype.charCodeAt(num)
例如:'abc'.charCodeAt(1) 得到97
// 4、判断当前字符串,是否包含指定字符串str。
String.prototype.includes(str)
// 5、判断某个字符串str,在当前字符串中第一个出现的下标位置。
String.prototype.indexOf(str)
// 6、判断某个字符串str,在当前字符串中最后一个出现的下标位置。
String.prototype.lastIndexOf(str)
// 7、判断当前字符串,是否以