day20 环境对象this、回调函数

环境对象

  • 能够分析判断函数运行在不同环境中 this 所指代的对象。

  • 环境对象指的是函数内部特殊的变量 this ,它代表着当前函数运行时所处的环境

  • 每个函数里都有this环境对象,普通函数里this指向的是window

  1. this 本质上是一个变量,数据类型为对象
  2. 函数的调用方式不同 this 变量的值也不同
  3. 谁调用 this 就是谁】是判断 this 值的粗略规则
  4. 函数直接调用时实际上 window.sayHi() 所以 this 的值为 window
<button>click</button>
    <script>
        const btn = document.querySelector('button')
        btn.addEventListener('click', function () {
   
            this.style.color = 'red'
        })
    </script>

回调函数

如果将函数A作为参数传递给函数B时,我们称函数A为回调函数

  1. 回调函数本质还是函数,只不过把它当成参数使用
  2. 使用匿名函数做为回调函数比较常见

举例:

    <script>
    	function fn() {
   
        console.log('我是回调函数...');
      }
      // 调用定时器
      setInterval(fn, 1000);
    </script>

fn 函数做为参数传给了setInterval ,这便是回调函数的实际应用了,结合刚刚学习的函数表达式上述代码还有另一种更常见写法。

    <script>
      // 调用定时器,匿名函数做为参数
      setInterval(function () {
   
        console.log('我是回调函数...');
      }, 1000);
    </script>

相关推荐

  1. day20 环境对象this函数

    2024-01-25 15:08:01       56 阅读
  2. 【vue函数中的 this 指向上】

    2024-01-25 15:08:01       36 阅读
  3. 函数详解

    2024-01-25 15:08:01       63 阅读
  4. ajax函数

    2024-01-25 15:08:01       29 阅读
  5. 函数(Language C)

    2024-01-25 15:08:01       49 阅读
  6. 函数的介绍

    2024-01-25 15:08:01       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-25 15:08:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-25 15:08:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-25 15:08:01       82 阅读
  4. Python语言-面向对象

    2024-01-25 15:08:01       91 阅读

热门阅读

  1. 【无标题】

    2024-01-25 15:08:01       52 阅读
  2. C++ 拷贝构造函数

    2024-01-25 15:08:01       54 阅读
  3. 2024.1.24力扣每日一题——美丽塔I

    2024-01-25 15:08:01       61 阅读
  4. Kong工作原理 - 负载均衡 - 基于DNS的负载均衡

    2024-01-25 15:08:01       53 阅读
  5. vue若依el-upload实现oss上传

    2024-01-25 15:08:01       51 阅读
  6. torch.nn.functional.pad()

    2024-01-25 15:08:01       64 阅读
  7. Node.js中fs模块

    2024-01-25 15:08:01       55 阅读
  8. Spring框架与反射

    2024-01-25 15:08:01       58 阅读
  9. QEMU搭建arm虚拟机开发环境

    2024-01-25 15:08:01       48 阅读
  10. Qt‘s 撤销框架(Qt‘s Undo Framework)

    2024-01-25 15:08:01       56 阅读
  11. Qt容器QVariant

    2024-01-25 15:08:01       55 阅读