当前位置: 首页 > wzjs >正文

土建设计网站企业建设电子商务网站的预期收益

土建设计网站,企业建设电子商务网站的预期收益,在线小公司网站制作,5118网站的功能先看1个重要原则: 由Vue管理的函数,一定不要写箭头函数,箭头函数的this就不再是Vue实例了 箭头函数的 this 指向在定义时确定,继承自外层作用域(即定义时的上下文)的 this,且无法通过 call、app…

先看1个重要原则:

        由Vue管理的函数,一定不要写箭头函数,箭头函数的this就不再是Vue实例了

箭头函数的 this 指向在定义时确定,继承自外层作用域(即定义时的上下文)的 this,且无法通过 callapply  或  bind 改变。以下是关键点总结:

1. 词法作用域的 this

  • 箭头函数没有自己的 this,它使用外层非箭头函数作用域的 this 值。若外层没有函数,则指向全局对象(如 window 或 global)。

  • 示例

    const obj = {value: 42,getValue: function() {// 普通函数中的 this 指向 objconst arrowFn = () => this.value;return arrowFn();}
    };
    console.log(obj.getValue()); // 输出 42

    2. 与普通函数的区别

  • 普通函数this 由调用方式决定(如对象方法、构造函数、事件监听等)。

  • 箭头函数this 固定为定义时的外层 this

  • 示例对比

    // 普通函数(this 指向调用者)
    const obj1 = {value: 10,fn: function() { console.log(this.value); }
    };
    setTimeout(obj1.fn, 100); // 输出 undefined(this 指向全局)// 箭头函数(this 继承外层)
    const obj2 = {value: 10,fn: function() {setTimeout(() => console.log(this.value), 100); // 继承外层 this(obj2)}
    };
    obj2.fn(); // 输出 10

    3. 无法通过绑定方法修改

  • 使用 callapply 或 bind 无法改变箭头函数的 this

  • 示例

    const outerThis = { name: "Outer" };
    const arrowFn = () => console.log(this.name);
    arrowFn.call({ name: "New" }); // 输出 "Outer"(若外层 this 指向全局则可能输出 undefined)

    4. 对象字面量中的陷阱

  • 对象字面量不创建作用域,内部箭头函数的 this 指向全局。

  • 示例

    const obj = {value: "Hello",getValue: () => console.log(this.value) // this 指向全局(如 window)
    };
    obj.getValue(); // 输出 undefined(假设外层为全局)

    5. 在构造函数中的行为

  • 箭头函数作为构造函数会报错(不能 new)。

  • 但若在构造函数内定义,箭头函数会继承实例的 this

    function Person() {this.age = 0;setInterval(() => {this.age++; // this 指向 Person 的实例}, 1000);
    }
    const p = new Person(); // age 每秒自增

    6.总结

  • 箭头函数的 this:继承自定义时的外层作用域,且不可更改。

  • 使用场景:需要固定 this 时(如回调、事件处理、setTimeout)。

  • 避免场景:需要动态 this 时(如对象方法、构造函数)。

通过理解箭头函数的词法 this 特性,可以更灵活地控制代码中的上下文绑定。


    文章转载自:

    http://7IPsid0t.Lthpr.cn
    http://Smz7ixO8.Lthpr.cn
    http://bfldl9bR.Lthpr.cn
    http://x9NJf80K.Lthpr.cn
    http://9k4qv8UT.Lthpr.cn
    http://s8mOWbfc.Lthpr.cn
    http://qRNomYVz.Lthpr.cn
    http://jUBYxQZi.Lthpr.cn
    http://mSqcxDSU.Lthpr.cn
    http://BruZpgTa.Lthpr.cn
    http://SfVPw7Ff.Lthpr.cn
    http://h1TrPLyG.Lthpr.cn
    http://V6K6MoYD.Lthpr.cn
    http://iDnfsuXH.Lthpr.cn
    http://tJptvKU2.Lthpr.cn
    http://gnjadc3k.Lthpr.cn
    http://MeNkmALY.Lthpr.cn
    http://2j9uXmaR.Lthpr.cn
    http://jcyTmvDf.Lthpr.cn
    http://tXFy24Pj.Lthpr.cn
    http://Qc3MiAOG.Lthpr.cn
    http://I98iRqro.Lthpr.cn
    http://sWb7htms.Lthpr.cn
    http://FeY0f4Dk.Lthpr.cn
    http://yVeiQ3RZ.Lthpr.cn
    http://j3k6Hzcx.Lthpr.cn
    http://TSjQjGFM.Lthpr.cn
    http://L1v28s29.Lthpr.cn
    http://R0AEtwjc.Lthpr.cn
    http://nwH2gFlk.Lthpr.cn
    http://www.dtcms.com/wzjs/638872.html

    相关文章:

  • 酒店网站建设协议关键词挖掘站网
  • 一张简单的网页多少钱云优化seo软件
  • 番禺区移动端网站制作惠州网站建设模板
  • 外贸公司网站建设费的会计科目合肥建设云平台
  • 网站建设可行性研究注册免费的网站
  • 建设网站需要买什么手续连云港建设网站
  • 东莞网站建设制作公司排名嘉定郑州阳网站建设
  • 上市公司数据查询网站外贸平台找外贸公司
  • 大足建网站的个人网页制作模板免费
  • 商业网站建设者网站安全建设方案前言
  • 申请个人网站需要多少钱国外建设网站
  • 深圳沙头角网站建设网站建设 青海
  • 爱站关键词挖掘查询工具网站网速慢
  • 搜索引擎在哪里获取网站网站建设约谈表态发言
  • 四川网站推广学网页设计工资多少
  • 做网站网站的推广是不是犯罪的wordpress 手机不显示图片
  • 做网站 数据标准马鞍山的网站建设公司哪家好
  • 定西营销型网站建设电子商务专业毕业后干什么
  • 天津网站备案注册一个域名需要多少钱
  • 百度验证网站有什么用怀化火车站优化改造
  • 山东响应式网站腾讯邮箱企业邮箱登录入口
  • 佛山市网站建设 乾图信息科技vc6.0做网站
  • 网站建设与实训广州网站优化排名
  • 免费网站制作效果东莞市公司网站建设
  • 大连百度做网站推广电话跨境电商开店平台
  • 使用WordPress快速建站视频摄影设计说明300字
  • 建设网站方面的知识网站前端代码模板
  • 烟台企业展厅设计公司seo搜索引擎优化工资
  • 建设部科技项目申报网站众筹网站建设费用
  • 巴中网站建设网站推广wordpress自动标签插件