码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 执行上下文,js、React、HTML中的this


    目录

    执行上下文:管理和执行代码

    属性:变量对象、this,作用域链

    变量对象是与执行上下文相关的数据作用域,存储:变量、函数声明

    生命周期

    创建:生成变量对象、创建函数作用域,建立作用域链、确定this的指向

    执行:变量赋值、函数的引用(调用时用指针)、执行其他代码

    分类:按执行时机

    全局执行上下文:this 指向window全局对象

    函数执行上下文:每次调用会创建新的执行上下文

    应用:()=>{}共享外部函数的执行上下文(this、性能优化)

    eval 执行上下文:动态创建的代码

    不推荐原因:XSS攻击、动态编译、维护性

    作用域:可访问变量的集合

    全局作用域

    函数作用域:在函数定义的时候就决定了

    块级作用域(ES6):{}

    作用链=作用域链表

    查找不到:原型链undefined,作用域链ReferenceError

    this:哪个对象调用就指向哪个,除非绑定(call、apply、bind)

    直接访问时(不作为对象的调用):调用对象与定义位置无关

    省略this时:指向window/undefined 严格模式

    JS

    非严格模式:对象

    严格模式:任意值

    改变this中的thisArg

    new 运算符构造绑定函数:提供的 this 值会被忽略(因为构造函数会准备自己的 this

    new.target

    原始值转换为对象:->Number/String

    全局对象替换:null/undefined->window(非严格模式)

    bind

    HTML

    React的class实例


    执行上下文:管理和执行代码

    属性:变量对象、this,作用域链

    1. const ExecutionContextObj = {
    2. VO: window, // 变量对象
    3. ScopeChain: {}, // 作用域链
    4. this: window
    5. };

    变量对象是与执行上下文相关的数据作用域,存储:变量、函数声明

    生成变量对象:

    • 创建arguments
    • 扫描函数声明
    • 扫描变量声明

    生命周期

    创建:生成变量对象、创建函数作用域,建立作用域链、确定this的指向

    执行:变量赋值、函数的引用(调用时用指针)、执行其他代码

    分类:按执行时机

    全局执行上下文:this 指向window全局对象

    函数执行上下文:每次调用会创建新的执行上下文

    应用:()=>{}共享外部函数的执行上下文(this、性能优化)

    eval 执行上下文:动态创建的代码

    不推荐原因:XSS攻击、动态编译、维护性

    1. //函数内部找不到就会去外层作用域
    2. function foo() {
    3. console.log(a);
    4. }
    5. function bar() {
    6. let a="bar"
    7. foo();
    8. }
    9. let a="window"
    10. bar(); //window

    浏览器调试工具

    作用域:可访问变量的集合

    作用域最大的用处就是隔离变量,不同作用域下同名变量不会有冲突

    全局作用域

    函数作用域:在函数定义的时候就决定了

    块级作用域(ES6):{}

    作用链=作用域链表

    查找不到:原型链undefined,作用域链ReferenceError

    this:哪个对象调用就指向哪个,除非绑定(call、apply、bind)

    直接访问时(不作为对象的调用):调用对象与定义位置无关

    1. var a=1;
    2. function fn1(){
    3. var a=2;
    4. console.log(this.a+a);
    5. }
    6. //f1并没有被作为对象的方法调用, this 指向全局对象,在浏览器中是 window
    7. fn1();//3
    8. function fn2(){
    9. var a=10;
    10. fn1();
    11. }
    12. //调用对象与定义位置无关
    13. fn2();//3

    省略this时:指向window/undefined 严格模式

    1. // 整个脚本都开启严格模式的语法
    2. "use strict";
    3. var v = "Hi! I'm a strict mode script!";

    JS

    当前执行上下文(global、function 或 eval)的一个属性:this

    可以使用 globalThis 获取全局对象,无论你的代码是否在当前上下文运行。

    非严格模式:对象

    严格模式:任意值

    如果进入执行环境时没有设置 this 的值,this 会保持为 undefined

    1. function f2() {
    2. "use strict"; // 这里是严格模式
    3. return this;
    4. }
    5. f2() === undefined; // true

    改变this中的thisArg

    new 运算符构造绑定函数:提供的 this 值会被忽略(因为构造函数会准备自己的 this

    new.target
    1. //如果构造函数是通过 new 运算符来调用的,则 new.target 将指向构造函数本身,否则它将是 undefined
    2. //new.target 是一个在构造函数中可用的元属性(meta-property),用于检查构造函数是如何被调用的。而 Base 是一个类(或构造函数)的名称
    3. class Base {
    4. constructor(...args) {
    5. console.log(new.target === Base);
    6. console.log(args);
    7. }
    8. }
    9. const BoundBase = Base.bind(null, 1, 2);
    10. new BoundBase(3, 4); // true, [1, 2, 3, 4]
    1. function Greet(name) {
    2. this.name = name;
    3. }
    4. const person = {
    5. name: "Alice"
    6. };
    7. // 使用 bind 创建绑定函数,将 this 设置为 person
    8. const boundGreet = Greet.bind(person, "Bob");
    9. // 使用 new 运算符尝试构造绑定函数
    10. const newGreet = new boundGreet();
    11. console.log(newGreet.name); // 输出 "Bob",而不是 "Alice"

    原始值转换为对象:->Number/String

    期望 this 是一个对象,但 thisArg 参数是一个原始值(比如数字、字符串等),则 thisArg 会被转换为对应的包装对象。例如,如果 thisArg 是一个数字,它将被转换为 Number 包装对象

    严格模式下,不允许将原始值(如字符串、数字、布尔值)包装为对应的对象(String、Number、Boolean),而是保持它们的原始类型

    1. "use strict"; // 防止 `this` 被封装到到包装对象中
    2. function log(...args) {
    3. console.log(this, ...args);
    4. }
    5. const boundLog = log.bind("this value", 1, 2);
    6. const boundLog2 = boundLog.bind("new this value", 3, 4);
    7. boundLog2(5, 6); // "this value", 1, 2, 3, 4, 5, 6
    8. //不用严格模式,则输出{"this value"}, 1, 2, 3, 4, 5, 6

    全局对象替换:null/undefined->window(非严格模式)

    如果 thisArg 参数传入了 null 或 undefined,在非严格模式下,它们会被替换为全局对象(通常是 window 对象)。这是为了确保函数始终有一个合法的 this 对象,防止出现错误。在严格模式下,null 或 undefined 不会被替换,函数内部的

    1. "use strict"; // 防止 `this` 被封装到到包装对象中
    2. function log(...args) {
    3. console.log(this, ...args);
    4. }
    5. const boundLog = log.bind("this value", 1, 2);
    6. const boundLog2 = boundLog.bind("new this value", 3, 4);
    7. boundLog2(5, 6); // "this value", 1, 2, 3, 4, 5, 6

    this 将保持为 null 或 undefined。

    bind

    1. const module = {
    2. x: 42,
    3. getX: function () {
    4. return this.x;
    5. },
    6. };
    7. const unboundGetX = module.getX;
    8. console.log(unboundGetX()); // The function gets invoked at the global scope
    9. // Expected output: undefined
    10. const boundGetX = unboundGetX.bind(module);
    11. console.log(boundGetX());
    12. // Expected output: 42

    绑定函数将绑定时传入的参数(包括 this 的值和前几个参数)提前存储为其内部状态。而不是在实际调用时传入。

    通常情况下,你可以将 const boundFn = fn.bind(thisArg, arg1, arg2) 和 const boundFn = (...restArgs) => fn.call(thisArg, arg1, arg2, ...restArgs) 构建的绑定函数的调用效果视为等效

    绑定函数可以通过调用 boundFn.bind(thisArg, /* more args */) 进一步进行绑定,从而创建另一个绑定函数 boundFn2。新绑定的 thisArg 值会被忽略,因为 boundFn2 的目标函数是 boundFn,而 boundFn 已经有一个绑定的 this 值了。

    HTML

    1. <button onclick="click(this)">传进去的为当前buttonbutton>
    2. <button onclick="click()">click()中直接使用this为windowbutton>

    React的class实例

      

    1. import React, { Component } from 'react'; // 请确保导入 React 和 Component
    2. class APP extends Component {
    3. constructor(props) {
    4. super(props);
    5. // 将 handleClick 方法绑定到组件实例的上下文
    6. this.handleClick5 = this.handleClick5.bind(this);
    7. }
    8. handleClick1(ev) {
    9. console.log(this);//undefined
    10. console.log(ev);//合成的SyntheticBaseEvent 
    11. console.log(ev.target);//button
    12. }
    13. //箭头函数
    14. //方法A:类中箭头
    15. handleClick2 = () => {
    16. console.log(this);//APP类组件实例
    17. }
    18. //方法B:onclick中箭头
    19. handleClick3() {
    20. console.log(this);//APP类组件实例
    21. }
    22. // bind绑定组件实例this
    23. // 方法A:onclick
    24. handleClick4() {
    25. console.log(this); //APP类组件实例
    26. }
    27. // 方法B:constructor
    28. handleClick5() {
    29. console.log(this); //APP类组件实例
    30. }
    31. render() {
    32. return (
    33. <div>
    34. <button onClick={this.handleClick1}>点击1button>
    35. {/* 箭头函数 */}
    36. <button onClick={this.handleClick2}>点击2button>
    37. <button onClick={() => { this.handleClick3() }}>点击3button>
    38. {/* bind */}
    39. <button onClick={this.handleClick4.bind(this)}>点击4button>
    40. <button onClick={this.handleClick5}>点击5button>
    41. div>
    42. );
    43. }
    44. }
    45. export default APP;

  • 相关阅读:
    SSM二手交易网站
    # Itext Pdf 合并拆分
    (2)paddle---简单线性回归和波士顿房价预测
    Three.js + Tensorflow.js 构建实时人脸点云
    TS编译选项
    基于知识蒸馏的两阶段去雨去雪去雾模型学习记录(三)之知识测试阶段与评估模块
    ASRT从零搭建并测试
    Spring Boot中的分布式缓存方案
    SQL Server —— While语句循环
    SAP UI5 初学 ( 一 )、简介
  • 原文地址:https://blog.csdn.net/qq_28838891/article/details/132926961
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号