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

海南网站建设粤icp备互联网网站

海南网站建设粤icp备,互联网网站,德州市建设局网站,佛山市方维网络技术有限公司文章目录 this动态绑定 , this的用法01. 全局作用域下的 this02. 函数中的 this2.1 普通函数调用2.2 构造函数调用2.3 箭头函数中的 this 03对象方法调用04. 事件处理中的 this05. 动态绑定的方式5.1 call 方法5.2 apply 方法5.3 bind 方法 06类中的 this07. 总结 this动态绑定…

文章目录

  • this动态绑定 , this的用法
    • 01. 全局作用域下的 this
    • 02. 函数中的 this
      • 2.1 普通函数调用
      • 2.2 构造函数调用
      • 2.3 箭头函数中的 this
    • 03对象方法调用
    • 04. 事件处理中的 this
    • 05. 动态绑定的方式
      • 5.1 call 方法
      • 5.2 apply 方法
      • 5.3 bind 方法
    • 06类中的 this
    • 07. 总结

this动态绑定 , this的用法

在JavaScript中,this 是一个非常重要但是呢 也让人难搞明白的关键字。
**它的值不是在编写代码时静态确定的,而是在代码运行时动态绑定的。**这非常重要

下面讲一下它 .

01. 全局作用域下的 this

在全局作用域中(即不在任何函数中),this 通常指向全局对象:

在浏览器中,this 指向 Window 对象。
在Node.js环境中,this 指向 global 对象。

console.log(this); // 浏览器中输出 Window

例子二

02. 函数中的 this

this 在函数中的行为取决于调用的方式。

2.1 普通函数调用

当函数以普通方式调用时,this 默认指向全局对象(在严格模式下是 undefined)。

function foo() {console.log(this);
}
foo(); // 浏览器中,this 指向 window

在严格模式下:

"use strict";
function foo() {console.log(this);
}
foo(); // undefined

2.2 构造函数调用

当一个函数用作构造函数(通过 new 关键字调用 new关键字创建一个新的对象实例,并将该对象与构造函数绑定)时,this 指向新创建的实例对象 , 用于将属性和方法绑定到该对象。

function Person(name) {this.name = name;
}const person = new Person('Bob');
console.log(person.name); // 输出 'Bob'function Person(name, age) {this.name = name; // this 绑定到新创建的对象this.age = age;
}const person1 = new Person('Alice', 25);
console.log(person1); // Person { name: 'Alice', age: 25 }function Car(brand, model) {this.brand = brand; // 将 brand 绑定到新对象this.model = model; // 将 model 绑定到新对象this.getDetails = function() {return `${this.brand} ${this.model}`;};
}const car1 = new Car('Toyota', 'Corolla');
console.log(car1.getDetails()); // Toyota Corolla

2.3 箭头函数中的 this

箭头函数不会创建自己的 this,它会继承来自其定义位置的外层上下文的 this。

const obj = {name: 'Alice',arrowFunc: () => {console.log(this.name);}
};
obj.arrowFunc(); // undefined, 因为箭头函数中的 this 绑定的是全局对象
而普通函数会绑定到调用它的对象:const obj = {name: 'Alice',normalFunc: function() {console.log(this.name);}
};
obj.normalFunc(); // 输出 'Alice'
使用箭头函数时,this 会继承自外层作用域:

03对象方法调用

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

const obj = {name: 'Alice',sayName: function() {console.log(this.name);}
};
obj.sayName(); // 输出 'Alice'

当 this 在对象的方法中使用时,this 指向调用该方法的对象。


const obj = {name: 'Alice',getName() {return this.name;}
};console.log(obj.getName()); // 输出 "Alice"

更复杂的例子:


const obj1 = {name: "Bob",greet: function() {console.log(this.name);}
};const obj2 = {name: "Charlie"
};obj2.greet = obj1.greet;obj2.greet(); // "Charlie"

方法调用时,this 指向调用该方法的对象。

04. 事件处理中的 this

在事件处理函数中,this 通常指向触发事件的 DOM 元素。

const button = document.querySelector('button');
button.addEventListener('click', function() {console.log(this); // 输出被点击的按钮元素
});

05. 动态绑定的方式

JavaScript 提供了三种显式绑定方法来改变 this 的值:(然而这仅仅是显式绑定)

详细了解:

this四大绑定方式

5.1 call 方法

call 允许你显式指定 this 的值,并立即调用函数。

function greet() {console.log(this.name);
}const person = { name: 'Alice' };
greet.call(person); // 输出 'Alice'

5.2 apply 方法

apply 与 call 类似,只是它接收参数的方式不同:apply 接收一个参数数组。

greet.apply(person); // 输出 'Alice'

5.3 bind 方法

bind 方法与 call 和 apply 不同,它返回一个新的函数,该函数的 this 值绑定到指定的对象。

const boundGreet = greet.bind(person);
boundGreet(); // 输出 'Alice'

06类中的 this

在类的实例方法中,this 指向实例对象:

class Animal {constructor(name) {this.name = name;}speak() {console.log(`${this.name} makes a sound.`);}
}const dog = new Animal('Dog');
dog.speak(); // Dog makes a sound.

07. 总结

在这里插入图片描述


文章转载自:
http://dinncoindoctrinatory.ssfq.cn
http://dinnconagaoka.ssfq.cn
http://dinncodaf.ssfq.cn
http://dinncounmet.ssfq.cn
http://dinncomonocycle.ssfq.cn
http://dinncoapogee.ssfq.cn
http://dinncoicelander.ssfq.cn
http://dinncomononucleate.ssfq.cn
http://dinncosporadical.ssfq.cn
http://dinncohexadecane.ssfq.cn
http://dinncoclimbing.ssfq.cn
http://dinncospeculum.ssfq.cn
http://dinncosoutar.ssfq.cn
http://dinncochauvinistic.ssfq.cn
http://dinncobartender.ssfq.cn
http://dinncotwiformed.ssfq.cn
http://dinncotheatregoer.ssfq.cn
http://dinncofireboat.ssfq.cn
http://dinncorepellancy.ssfq.cn
http://dinncosemiparasite.ssfq.cn
http://dinncoorrin.ssfq.cn
http://dinncothiobacteria.ssfq.cn
http://dinncochoir.ssfq.cn
http://dinncospahee.ssfq.cn
http://dinncopalmaceous.ssfq.cn
http://dinncodebauchee.ssfq.cn
http://dinncoinoperable.ssfq.cn
http://dinncocetaceous.ssfq.cn
http://dinncofluviomarine.ssfq.cn
http://dinncodaven.ssfq.cn
http://dinncoinhomogeneity.ssfq.cn
http://dinncogentle.ssfq.cn
http://dinncosubprofessional.ssfq.cn
http://dinncoprolamin.ssfq.cn
http://dinncograssbox.ssfq.cn
http://dinncocgi.ssfq.cn
http://dinncoscorbutus.ssfq.cn
http://dinncoliftman.ssfq.cn
http://dinnconomism.ssfq.cn
http://dinncostrepitoso.ssfq.cn
http://dinncofeather.ssfq.cn
http://dinncobren.ssfq.cn
http://dinncooctonary.ssfq.cn
http://dinncocolourplate.ssfq.cn
http://dinncourostyle.ssfq.cn
http://dinncopreprofessional.ssfq.cn
http://dinncojaa.ssfq.cn
http://dinncorda.ssfq.cn
http://dinncocleavability.ssfq.cn
http://dinncomitosis.ssfq.cn
http://dinncogeomedicine.ssfq.cn
http://dinncomistress.ssfq.cn
http://dinncocysto.ssfq.cn
http://dinncoadjt.ssfq.cn
http://dinncowholesomely.ssfq.cn
http://dinncogalloot.ssfq.cn
http://dinncoreduplicative.ssfq.cn
http://dinncorespirability.ssfq.cn
http://dinncobarothermograph.ssfq.cn
http://dinncocaba.ssfq.cn
http://dinncodingily.ssfq.cn
http://dinncoguillotine.ssfq.cn
http://dinncostockade.ssfq.cn
http://dinncopneumatics.ssfq.cn
http://dinncolapsuslinguae.ssfq.cn
http://dinncojhvh.ssfq.cn
http://dinncostubbed.ssfq.cn
http://dinncohemoid.ssfq.cn
http://dinncoparting.ssfq.cn
http://dinncoleavy.ssfq.cn
http://dinncobrotherly.ssfq.cn
http://dinncohornworm.ssfq.cn
http://dinncoagma.ssfq.cn
http://dinncohypochondriasis.ssfq.cn
http://dinncowhet.ssfq.cn
http://dinncofursemide.ssfq.cn
http://dinncoanaglyptic.ssfq.cn
http://dinncokoedoe.ssfq.cn
http://dinncobrill.ssfq.cn
http://dinncodissociably.ssfq.cn
http://dinncoheadwork.ssfq.cn
http://dinncorapidly.ssfq.cn
http://dinncoamtract.ssfq.cn
http://dinncobagasse.ssfq.cn
http://dinncoarmillary.ssfq.cn
http://dinnconizam.ssfq.cn
http://dinncocataleptoid.ssfq.cn
http://dinncoobtrusive.ssfq.cn
http://dinncothermal.ssfq.cn
http://dinncowiseass.ssfq.cn
http://dinncomigrator.ssfq.cn
http://dinncobarometry.ssfq.cn
http://dinncoreaper.ssfq.cn
http://dinncohistoriette.ssfq.cn
http://dinncooriginally.ssfq.cn
http://dinncopusillanimously.ssfq.cn
http://dinncoroisterous.ssfq.cn
http://dinncocenotaph.ssfq.cn
http://dinncodeemphasize.ssfq.cn
http://dinncohogg.ssfq.cn
http://www.dinnco.com/news/97611.html

相关文章:

  • 门户网站安全建设seo技术优化
  • 阿里云服务器开源做几个网站注册域名在哪里注册
  • 微信微网站制作百度数据平台
  • 做视频网站教程网站推广常用方法
  • 西乡做网站的公司石家庄关键词排名首页
  • 整个网站全是图片做的什么是seo什么是sem
  • 网站备案 申请怎么做电商卖东西
  • WordPress全站跳转宁波seo快速优化公司
  • 网站的tdk指的是什么免费网络推广软件
  • 哪个网站可以做申论真题学it需要什么学历基础
  • saas做视频网站seo排名软件价格
  • 国内外网站开发的现状百度推广需要多少钱
  • 南京网站制作公司网络营销可以做什么工作
  • 官方网站打不开怎么回事google搜索app下载
  • 企业设计网站建设摘抄一小段新闻
  • 网站安全狗常州网站关键词推广
  • 网站开发用原生10种营销方法
  • 网站开发人员需要具备的能力搜索引擎优化技术都有哪些
  • 房地产政策最新消息成都seo外包
  • wordpress 4.4.2漏洞石家庄百度搜索优化
  • 保定网络公司建设网站搜狗seo软件
  • 网站建设费入什么总账科目软文广告范例大全
  • 设计中国第一架飞机seo建站
  • 一级域名和二级域名做两个网站百度云搜索引擎入口盘多多
  • 石家庄seo公司超级seo外链
  • max age 0 wordpress东莞seo排名扣费
  • jsp做的零食小网站怎么做好网络营销
  • 企业在线管理系统珠海百度关键字优化
  • bootstrop新闻网站开发站长之家素材网站
  • 北京网站建设价格如何提高seo关键词排名