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

做海报哪个网站的素材多wordpress怎么翻页

做海报哪个网站的素材多,wordpress怎么翻页,seo关键词优化公司,购物网站开发的背景和意义在js中,this关键字的指向是一个比较重要的概念,它的值取决于函数的调用方式。 全局状态下 //全局状态下 this指向windowsconsole.log("this", this);console.log("thiswindows", this window); 在函数中 // 在函数中 this指向win…

在js中,this关键字的指向是一个比较重要的概念,它的值取决于函数的调用方式。

全局状态下

//全局状态下 this指向windowsconsole.log("this", this);console.log("this===windows", this === window);

在函数中

 // 在函数中 this指向windowsfunction text() {console.log("在函数中this", this);}text();

函数作为对象的方法被调用 

   // 函数作为对象的方法被调用 this指向该方法的对象const info = {name: "info",infoFunc: function () {console.log("info对象中this指向该对象", this, this.name);},};

 

构造函数中 

    // 构造函数中 this指向新创建的对象function Person(name) {this.name = name;console.log("构造函数", this);}const person = new Person("zs");

 

 函数嵌套函数,使用变量存储this,箭头函数

  const info2 = {name: "alice",// 函数嵌套函数 this 指向windowsinfo2Fun2: function () {function fun2() {console.log("函数嵌套函数中", this);}fun2();},info2Fun3: function () {//为了解决函数嵌套函数this指向windows的问题//将当前方法中的 this 值(也就是 info2 对象)存储到 _this 变量中var _this = this;function fun3() {console.log("使用_this存储this", _this);}fun3();},// 使用箭头函数也可以解决函数嵌套函数 info2Fun4: function () {// 箭头函数没有this,向上寻找则为info2对象的thisconst fun2 = () => {console.log("info2Func2方法中的fun2函数", this);};fun2();},};info2.info2Fun2();info2.info2Fun3();info2.info2Fun4();

 

 

显示修改this指向

在 JavaScript 中,apply、call 和 bind 是 Function 对象的三个重要方法,它们主要用于改变函数内部 this 的指向,同时还能在调用函数时传递参数

call

   function greet(message) {console.log(`${message}, ${this.name}`);}const person2 = { name: "John" };greet(person2, "Hello1111");greet.call(person2, "Hello");

不加call为什么是[object Object], 

普通函数调用的时候 this指向全局对象,在这个调用里面,把person2作为参数传递给greet函数,但函数内部不指向perosn2。greet函数只接收一个参数,当调用的时候,message被赋值为perosn2,第二个参数就被忽略了。全局对象的name为空字符串,同时message是person2对象,对象默认的字符串表示形式是 [object Object]  所以打印结果为[object Object]

  console.log('window.name',window.name,typeof(window.name));

 

加call

call方法第一个参数就是要绑定给this的对象,后续参数依次传递给被调用的函数,当调用的时候,this被显示地绑定到person2对象上,message被赋值为Hello

call方法支持传入多个参数,以逗号隔开

     function greet(message,message2) {console.log(`${message}, ${this.name},${message2}`);}const person2 = { name: "John" };greet.call(person2, "Hello","aaaaa");

 

apply 

apply与call类似,第一个参数都是指定函数执行时this的值,第二个参数也是传递数据用的,区别是它是以数组元素的形式传入的,多个参数则传入多个元素。当不确定参数数量的时候使用apply更方便。

   greet.apply(person2,['BBBBBBBB','CCCCC'])

 

bind

bind与call和aplly类似,第一个参数也是指定函数执行时this的值,区别是call和apply会立即执行,而bind会返回一个新的函数供后续使用

   const greet2=greet.bind(person2,'DDDDDD','EEEEE');greet2();

 

   const greet3=greet.bind(person2,'FFFFF','GGGGGG');greet3();

 

vue中的this

选项式API

在生命周期,methods,computed,watch中,this都指向组件实例

<template><div class="box"><div @click="addNum">A:{{ num }}</div><div @click="addNum2">B:{{ num }}</div><div @click="addNum2">C:{{ num2}}</div></div>
</template><script>
export default {data(){return {num:10}},computed:{num2(){console.log('computed',this);return this.num;}},watch:{num(newvalue,oldvalue){console.log('watch',this);      console.log(newvalue,oldvalue);}},created(){console.log('created中this',this);},mounted(){console.log('mounted中this',this);},methods:{addNum(){console.log('methods',this);this.num++;},addNum2:()=>{console.log('箭头函数',this);}}}
</script>

组合式API

组合式 API 鼓励将逻辑封装在函数中,这些函数可以在setup函数中调用。通过这种方式,逻辑更加模块化,作用域更加清晰。不再依赖this来访问实例的属性和方法,而是直接从返回的对象中解构出所需的变量和函数。

END 

 

http://www.dtcms.com/a/567966.html

相关文章:

  • 阿里云网站备案登陆怎么设计网页模板
  • 备案注销网站还有吗网站买流量是怎么做的
  • 东莞h5网站建设工程建设标准化期刊网站
  • 手机网站域名m打头有专门做面包的网站么
  • 网站一元空间有哪些呀wordpress安装使用视频教程
  • 新乡做企业网站的公司用ip的网站要备案吗
  • 关于网站开发人员的薪资大学生应届毕业生招聘官网
  • 光谷企业网站建设爬虫做视频网站
  • 文山建设局网站石家庄外贸网站制作
  • 网站开发交付验收文档为什么做网站越早越好
  • 企业网站建设外包白银市城乡建设局网站
  • 网站跳转怎么做360做类似淘宝的网站
  • 毕业网站建设pptwordpress 培训模板下载
  • vs2008不能新建网站清远做网站seo
  • 简单的网站设计怎么做wordpress防止博客恶意采集
  • 电子商务网站建设软件湖南网站建设价位
  • 影视网站策划书7k7k网页游戏入口
  • 合肥网站建设模板商丘seo公司
  • 方圆网 网站建设简历制作哪里好
  • 做pc网站会连带手机版做摄影网站
  • 网站中flash企业管理系统网站开发标书
  • 淮安官方网站开发区建材网站设计
  • 婺源做网站有吗百度seo规则
  • 用ps制作网站首页jsp网站开发环境配置
  • 镇江网站建设制作企业二维码制作网站
  • 农村网站建设必要性手机可以建立网站吗
  • 海南网站制作公司优化公司排行榜
  • 网站优化升级网站怎么制作教程
  • 网站定制开发收费标准是多少汕头建站模板厂家
  • 上海专业网站设计制作网站建设 上海浦东