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

找高权重的网站做外链网页制作培训心得体会

找高权重的网站做外链,网页制作培训心得体会,宝塔建设网站教程,建设网站可选择的方案有在 JavaScript 中,this 关键字的指向可能是一个令人困惑的概念。它不像其他编程语言那样固定,而是取决于函数的调用方式。理解 this 的指向是掌握 JavaScript 的关键之一,本文将从基础概念开始,逐步深入到 Vue 框架中的特殊情况。…

在 JavaScript 中,this 关键字的指向可能是一个令人困惑的概念。它不像其他编程语言那样固定,而是取决于函数的调用方式。理解 this 的指向是掌握 JavaScript 的关键之一,本文将从基础概念开始,逐步深入到 Vue 框架中的特殊情况。

一、JavaScript 中 this 的基础指向规则

1. 全局上下文

在全局作用域中,this 指向全局对象。在浏览器环境中,这个全局对象是 window

console.log(this === window); // true// 定义一个全局变量
var globalVar = 'Hello, this is global';
console.log(this.globalVar); // 'Hello, this is global'
console.log(window.globalVar); // 'Hello, this is global'// 普通函数中的 this
function showThis() {console.log(this); // window
}
showThis();

2. 函数作为对象方法调用

当函数作为对象的方法被调用时,this 指向调用该方法的对象。

const person = {name: 'Alice',greet: function() {console.log(`Hello, my name is ${this.name}`);}
};person.greet(); // 'Hello, my name is Alice'

即使方法被赋值给另一个变量,this 的指向也会根据调用方式变化:

const greetFunc = person.greet;
greetFunc(); // 'Hello, my name is undefined'(在严格模式下 this 为 undefined)

3. 构造函数调用

当函数通过 new 关键字作为构造函数调用时,this 指向新创建的对象。

function Car(make, model) {this.make = make;this.model = model;this.getInfo = function() {return `${this.make} ${this.model}`;};
}const myCar = new Car('Toyota', 'Corolla');
console.log(myCar.getInfo()); // 'Toyota Corolla'

4. 箭头函数的特殊性

箭头函数不绑定自己的 this,它继承自定义时的上下文。这使得箭头函数在处理回调函数时特别有用。

const counter = {count: 0,increment: function() {// 使用箭头函数,this 继承自 increment 方法的 thissetTimeout(() => {this.count++;console.log(this.count);}, 1000);}
};counter.increment(); // 1(一秒后输出)

对比普通函数:

const counter = {count: 0,increment: function() {// 普通函数的 this 指向全局对象或 undefined(严格模式)setTimeout(function() {this.count++;console.log(this.count);}, 1000);}
};counter.increment(); // NaN(在浏览器中,全局对象的 count 属性未定义)

5. 显式绑定 this

JavaScript 提供了三种方法来显式绑定函数的 thiscall()apply() 和 bind()

function greet(message) {console.log(`${message}, ${this.name}`);
}const person1 = { name: 'Bob' };
const person2 = { name: 'Charlie' };// 使用 call
greet.call(person1, 'Hello'); // 'Hello, Bob'// 使用 apply
greet.apply(person2, ['Hi']); // 'Hi, Charlie'// 使用 bind 创建一个新函数
const greetBob = greet.bind(person1);
greetBob('Hey'); // 'Hey, Bob'

二、Vue 框架中的 this 指向

1. 选项式 API 中的 this

在 Vue 2.x 和 Vue 3 的选项式 API 中,this 指向组件实例。

import { createApp } from 'vue';const app = createApp({data() {return {message: 'Hello from Vue!'};},methods: {showMessage() {console.log(this.message); // 指向组件实例},delayedShow() {setTimeout(() => {console.log(this.message); // 箭头函数,this 仍然指向组件实例}, 1000);}}
});

2. 组合式 API 中的 this

在 Vue 3 的组合式 API 中,setup 函数没有自己的 this 上下文。状态和方法需要通过返回值暴露。

import { ref, onMounted } from 'vue';export default {setup() {const count = ref(0);const increment = () => {count.value++;};onMounted(() => {console.log('Component mounted');});// 返回需要暴露给模板的内容return {count,increment};}
};

3. 特殊情况注意事项

在 Vue 组件中使用原生事件处理函数或第三方库时,需要特别注意 this 的指向:

export default {data() {return {items: []};},mounted() {// 错误示例:this 指向 window 或 undefined(严格模式)document.addEventListener('click', function() {this.items.push('new item'); // 错误!this 不指向组件实例});// 正确示例:使用箭头函数document.addEventListener('click', () => {this.items.push('new item'); // 正确!this 指向组件实例});}
};

三、常见陷阱与最佳实践

1. 回调函数中的 this 丢失

这是最常见的问题之一,解决方案包括:

  • 使用箭头函数
  • 使用 bind() 方法
  • 保存 this 到变量(如 const self = this

现象
回调函数独立调用时,其 this 会指向 调用者上下文(如全局对象或 undefined),而非定义时的对象。

const obj = {name: "test",fn: function() {setTimeout(function() {console.log(this.name); // 非严格模式下指向 window,输出 undefined}, 100);}
};
obj.fn(); // 回调函数中 this 丢失

原因
回调函数的调用方式(如定时器、事件监听)决定了其 this 指向,与定义时的对象无关。

解决方法

用箭头函数(继承外层 this):

setTimeout(() => { console.log(this.name); }, 100); // 正确指向 obj

用 bind/call/apply 绑定 this

setTimeout(function() {}.bind(this), 100); // 显式绑定 this 到 obj

2. 严格模式的影响

在严格模式下,全局作用域中的 this 是 undefined,而不是全局对象:

'use strict';function showThis() {console.log(this); // undefined
}showThis();

3. 最佳实践

  • 当需要保留上下文时,优先使用箭头函数
  • 当需要动态绑定上下文时,使用 call()apply() 或 bind()
  • 在 Vue 组合式 API 中,避免依赖 this,使用组合式 API 提供的工具(如 ref、reactive、computed 等)来管理状态。

总结

理解 JavaScript 中 this 的指向需要结合函数的调用方式和上下文环境。从全局上下文到对象方法,从构造函数到箭头函数,this 的指向灵活多变。在 Vue 框架中,this 的行为也因 API 风格而异。通过掌握这些规则和最佳实践,可以更好地编写 JavaScript 代码,避免因 this 指向问题导致的错误。


文章转载自:

http://jUlhPo0W.Ltqtp.cn
http://asTHJKDE.Ltqtp.cn
http://CaL6IVJQ.Ltqtp.cn
http://HSvYKoWY.Ltqtp.cn
http://OQ3LYjEE.Ltqtp.cn
http://ZwigK5dY.Ltqtp.cn
http://viQAIuli.Ltqtp.cn
http://tmeFkAiS.Ltqtp.cn
http://owFTSxxp.Ltqtp.cn
http://R70KvSev.Ltqtp.cn
http://ZhbAZLZ6.Ltqtp.cn
http://2hT6mHgW.Ltqtp.cn
http://ViEi4lJG.Ltqtp.cn
http://wxuGrsHS.Ltqtp.cn
http://xuOIAnfQ.Ltqtp.cn
http://PWixZ4cn.Ltqtp.cn
http://iqJnp66I.Ltqtp.cn
http://HVzghiO2.Ltqtp.cn
http://96JFaUrT.Ltqtp.cn
http://PHrhOr49.Ltqtp.cn
http://FZeWPaAx.Ltqtp.cn
http://Qp8780ES.Ltqtp.cn
http://V30wd7wz.Ltqtp.cn
http://jCxksGnP.Ltqtp.cn
http://cWvJc40T.Ltqtp.cn
http://Ndk6zd9P.Ltqtp.cn
http://F3PPBwrp.Ltqtp.cn
http://fG2UP2K8.Ltqtp.cn
http://Hh8DHbNb.Ltqtp.cn
http://RcueyrBM.Ltqtp.cn
http://www.dtcms.com/wzjs/729828.html

相关文章:

  • 那个网站做外贸网站做数据分析
  • 连云港网站优化方案网站网址注册
  • 专业网站建设搭建大数据分析营销平台
  • 营销型网站的评价标准多少钱做网站
  • 做网站的公司苏州国外 素材 网站
  • wordpress插件一键安装教程镇江网站建设优化排名
  • 化妆品行业网站建设网站开发专业有哪些
  • 新手做网站什么内容比较好烟台商城网站制作
  • 微网站 获取手机号在线设计网站免费
  • 文化传媒 网站设计saas小程序开发
  • 南通网站建设报价中国空间站实时位置
  • 个人网页设计作品html文件夹网络seo关键词优化技巧
  • 中国建设银行甘肃省分行 官方网站网站建设与管理策划书
  • 做网址导航网站收益兰州网络科技公司有哪些
  • php做网站后台教程网页设计模板套用步骤
  • 分析网站示例广东建设网站公司
  • 做的比较好的企业网站wordpress竖文
  • 网站推广投放东莞网站搭建找哪里
  • 一个专门做预告片的网站易企秀可以做网站吗
  • 聊城招聘网站建设做网站效果怎么样
  • 如何app开发制作深圳做网站优化
  • 网站建设 数据可视化wordpress蒲公英代码
  • 保定专业做网站建立自我追求无我是什么意思
  • 电子商务平台网站建造东莞市网络营销公司
  • 个人如何建立免费网站iis网站配置教程
  • 塔城地区建设工程信息网站北京快三
  • 提高网站浏览量做众筹的网站
  • 网站设计方案范本官方网站welcome怎么注册
  • 网站建设空间多大qq轻聊版下载
  • 手机网站开发模板制作一个购物网站需要多少钱