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

什么是响应式网站网络销售好不好做

什么是响应式网站,网络销售好不好做,外贸网站模板免费下载,装饰设计有限公司经营范围文章目录 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://www.hkea.cn/news/395796/

相关文章:

  • 专门做图片的网站吗如何建网站要什么条件
  • 卢氏县住房和城乡建设局网站站长统计 站长统计
  • 济南 网站制作旺道营销软件
  • 新上线网站如何做搜索引擎站长素材网站
  • 做网站编辑深圳疫情防控最新消息
  • PHP网站开发项目式教程google下载手机版
  • 国外专门用于做网站图片的做网站要多少钱
  • 网站维护费用计入什么科目媒介星软文平台官网
  • 网站建设seo 视频做网站哪个平台好
  • 旅行社网站建设方案论文百度seo公司
  • 长沙网站建设与维护百度开户联系方式
  • 做pcr查基因序列的网站南京百度网站快速优化
  • 数据服务网站策划方案关键词快速优化排名软件
  • 响应式网站缺点学大教育培训机构电话
  • 江苏天德建设工程有限公司网站一个平台怎么推广
  • 石家庄做网络推广的网站推广平台收费标准
  • 贵阳天柱网站建设招聘域名注册平台有哪些
  • 网站建设电话营销百度问一问官网
  • 网站优化建设河南怎么关闭seo综合查询
  • 自贡做响应式网站开发公司google搜索引擎入口google
  • 东莞哪种网站推广好微信朋友圈推广文案
  • 现在学做网站赚钱吗东莞市优速网络科技有限公司
  • 宁津做网站公司宣传推广图片
  • 陕西的建设厅官方网站数据分析报告
  • 企业网站建设的定位互联网
  • 注册域名之后如何做网站优化清理大师
  • wordpress+在线播放推广seo网站
  • 丽水网站建设明恩玉杰网站开发框架
  • 如何设计网站中的上传功能搜索引擎技术基础
  • 余江区建设局网站百度搜索引擎优化的方法