undefined和null
console.log(typeof undefined);
console.log(typeof null);
都表示的是无
但是null表示的是无 对象 0
undefined表示的是无的原始值
undefined的情况
// 几种情况
let o
console.log(o); // undefined
// 已经声明没有赋值
// 对象的某个属性不存在
let obj = {}
console.log(obj.name);
// 3.少传参
function fn(a, b) {
console.log(a, b);
}
fn(4) // 4 undefined
// 4.函数的默认返回值undefined
function fn1() {
return
}
console.log(fn1());
null的情况
// null
// 手动释放内存
let obj = {}
obj = null
// 2.作为函数的参数
// 此参数不是对象
// 3.null是原型链的顶端
过滤器分析
let a = [1, 2, 3, 4, 5]
let b = a.filter((current, index, array) => {
console.log(current);
console.log(index);
console.log(array);
return current > 2
})
console.log(b);
// 1.current当前值
// 2.index当前索引
// 3.array当前数组对象
forEach与map()
let arr = ["1", "2", "33"]
let res = arr.forEach(element => {
return element + "哈哈"
})
console.log(res);
// 得到了一个undefined
// foreacth是没有返回值的
// forEach是不能break的
// 遍历的具体值是value值
// map
let arr1 = ["1", "2", "33"]
let res1 = arr1.map((value, key) => {
return value + "111"
})
console.log(res1);
// map有返回值的
// 返回值就是一个数组 默认return是undefined
// 接收参数是一个函数(value,key)
// 不能用break打断
JS实现递归
// 递归求和1-100
function add(num1, num2) {
let num = num1 + num2
if (num2 + 1 > 100) {
return num
}
return add(num, num2 + 1)
}
let sum = add(1, 2)
console.log(sum);