当前位置:   article > 正文

JS面试题第一章

JS面试题第一章

undefined和null

  1. console.log(typeof undefined);
  2. console.log(typeof null);

都表示的是无

但是null表示的是无 对象 0

undefined表示的是无的原始值

undefined的情况

  1. // 几种情况
  2. let o
  3. console.log(o); // undefined
  4. // 已经声明没有赋值
  5. // 对象的某个属性不存在
  6. let obj = {}
  7. console.log(obj.name);
  8. // 3.少传参
  9. function fn(a, b) {
  10. console.log(a, b);
  11. }
  12. fn(4) // 4 undefined
  13. // 4.函数的默认返回值undefined
  14. function fn1() {
  15. return
  16. }
  17. console.log(fn1());

null的情况

  1. // null
  2. // 手动释放内存
  3. let obj = {}
  4. obj = null
  5. // 2.作为函数的参数
  6. // 此参数不是对象
  7. // 3.null是原型链的顶端

过滤器分析

  1. let a = [1, 2, 3, 4, 5]
  2. let b = a.filter((current, index, array) => {
  3. console.log(current);
  4. console.log(index);
  5. console.log(array);
  6. return current > 2
  7. })
  8. console.log(b);
  9. // 1.current当前值
  10. // 2.index当前索引
  11. // 3.array当前数组对象

forEach与map()

  1. let arr = ["1", "2", "33"]
  2. let res = arr.forEach(element => {
  3. return element + "哈哈"
  4. })
  5. console.log(res);
  6. // 得到了一个undefined
  7. // foreacth是没有返回值的
  8. // forEach是不能break的
  9. // 遍历的具体值是value值
  10. // map
  11. let arr1 = ["1", "2", "33"]
  12. let res1 = arr1.map((value, key) => {
  13. return value + "111"
  14. })
  15. console.log(res1);
  16. // map有返回值的
  17. // 返回值就是一个数组 默认return是undefined
  18. // 接收参数是一个函数(value,key)
  19. // 不能用break打断

JS实现递归

  1. // 递归求和1-100
  2. function add(num1, num2) {
  3. let num = num1 + num2
  4. if (num2 + 1 > 100) {
  5. return num
  6. }
  7. return add(num, num2 + 1)
  8. }
  9. let sum = add(1, 2)
  10. console.log(sum);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/757821
推荐阅读
相关标签
  

闽ICP备14008679号