-
前言
- 0. 前端工具
- 1. html
- 2. CSS
-
3. JavaScript 基础
-
3.1 JS简介
-
3.2 变量
-
3.3 变量的强制类型转换
-
3.4 运算符
-
3.5 流程控制语句:选择结构(if和switch)
-
3.6 流程控制语句:循环结构(for和while)
-
3.7 对象简介和对象的基本操作
-
3.8 基本数据类型vs引用数据类型
-
3.9 函数
-
3.10 作用域
-
3.11 this
-
3.12 对象的创建&构造函数
-
3.13 原型对象
-
3.14 数组简介
-
3.15 数组的四个基本方法&数组的遍历
-
3.16 数组的常见方法
-
3.17 数组的其他方法
-
3.18 内置对象:Date
-
3.19 内置对象(其他)
-
3.20 DOM操作
-
3.21 事件对象Event和冒泡
-
3.22 事件委托
-
3.23 BOM的常见内置方法和内置对象
-
3.24 原型链
-
3.25 常见代码解读
-
3.1 JS简介
- 4. JavaScript进阶
- 5. 前端基本功:CSS、DOM练习和 Javascript 特效
- 6. jQuery
- 7. HTML5和CSS3
- 8. 移动web开发
- 9. Ajax
- 10. Node.js和模块化
- 11. ES6
- 12. Vue基础
- 13. React基础
-
14. 前端面试
-
14.1 准备
-
14.2 页面布局
-
14.3 CSS盒模型及BFC
-
14.4 DOM事件的总结
-
14.5 HTTP协议
-
14.6 创建对象和原型链
-
14.7 面向对象:类的定义和继承的几种方式
-
14.8 跨域通信类
-
14.9 安全问题:CSRF和XSS
-
14.10 算法问题
-
14.11 浏览器渲染机制
-
14.12 js运行机制:异步和单线程
-
14.13 页面性能优化
-
14.14 前端错误监控
-
14.15 JavaScript高级面试:前言
-
14.16 模块化的使用和编译环境
-
14.17 ES6
-
14.18 虚拟DOM
-
14.19 MVVM
-
14.20 面试题整理
-
14.21 网友面经
-
14.22 面试技巧
-
14.23 推荐文章
-
14.24 计算机网络
-
14.25 web安全
-
14.26 其他
-
14.1 准备
- 15. 面试题积累
- 16. 前端进阶
- 17. 前端综合
- 18. 推荐链接
前言
我们先来看下面这段代码:
components/MyComponent.jsx
import React from "react";
export default class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
msg: "这是 MyComponent 组件 默认的msg"
};
}
render() {
return (
<div>
<h1>绑定This并传参</h1>
<input type="button" value="绑定this并传参" onClick={this.changeMsg} />
<h3>{this.state.msg}</h3>
</div>
);
}
changeMsg() {
// 注意:这里的changeMsg()只是一个普通方法。因此,在触发的时候,这里的 this 是 undefined
console.log(this); // 打印结果:undefined
this.setState({
msg: "设置 msg 为新的值"
});
}
}
copy
上面的代码中,点击按钮,执行 changeMsg() 方法,尝试修改 this.state.msg 的值。但是,这个方法执行的时候,是会报错的:
Uncaught TypeError: Cannot read property 'setState' of null
copy
而且,打印this的结果也是 undefined。这是为啥呢?因为这里的 this 并不是指向 MyComponent 组件本身。
那如何让 changeMsg() 方法里面的 this,指向MyComponent 组件呢?办法总是有的,比如说,将changeMsg() 修改为箭头函数:
changeMsg = () => {
console.log(this); // 打印结果:MyComponent 组件
this.setState({
msg: "设置 msg 为新的值"
});
};
copy
那么,除了箭头函数可以 绑定 this,还有没有其他的方式呢?我们接下来讲一讲。
绑定 this 的方式一:bind()
代码举例:
import React from "react";
export default class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
msg: "这是 MyComponent 组件 默认的msg"
};
}
render() {
return (
<div>
<h1>绑定This并传参</h1>
{/* bind 的作用:为前面的函数,修改函数内部的 this 指向。让 函数内部的this,指向 bind 参数列表中的 第一个参数 */}
<input
type="button"
value="绑定this并传参"
onClick={this.changeMsg1.bind(this)}
/>
<h3>{this.state.msg}</h3>
</div>
);
}
changeMsg1() {
this.setState({
msg: "设置 msg 为新的值"
});
}
}
copy
上方代码中,我们为什么用 bind(),而不是用 call/apply 呢?因为 bind() 并不会立即调用,正是我们需要的。
注意:bind 中的第一个参数,是用来修改 this 指向的。第一个参数后面的所有参数,都将作为函数的参数传递进去。
我们来看看通过 bind() 是怎么传参的。
通过 bind() 绑定this,并给函数传参:
import React from "react";
export default class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
msg: "这是 MyComponent 组件 默认的msg"
};
}
render() {
return (
<div>
<h1>绑定This并传参</h1>
{/* bind 的作用:为前面的函数,修改函数内部的 this 指向。让 函数内部的this,指向 bind 参数列表中的 第一个参数 */}
<input type="button" value="绑定this并传参" onClick={this.changeMsg1.bind(this, "千古啊", "壹号啊")} />
<h3>{this.state.msg}</h3>
</div>
);
}
changeMsg1(arg1, arg2) {
this.setState({
msg: "设置 msg 为新的值" + arg1 + arg2
});
}
}
copy
绑定 this 并给函数传参 的方式二:构造函数里设置 bind()
我们知道,构造函数中的 this 本身就是指向组件的实例的,所以,我们可以在这里做一些事情。
代码举例:
import React from "react";
export default class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
msg: "这是 MyComponent 组件 默认的msg"
};
// 绑定 this 并给函数传参的方式2: 在构造函数中绑定并传参
// 注意:当一个函数调用 bind 改变了this指向后,bind 函数调用的结果,有一个【返回值】,这个值,就是被改变this指向后的函数的引用。
// 也就是说: bind 不会修改 原函数的 this 指向,而是改变了 “函数拷贝”的this指向。
this.changeMsg2 = this.changeMsg2.bind(this, "千古恩", "壹号恩");
}
render() {
return (
<div>
<h1>绑定This并传参</h1>
<input type="button" value="绑定this并传参" onClick={this.changeMsg2} />
<h3>{this.state.msg}</h3>
</div>
);
}
changeMsg2(arg1, arg2) {
this.setState({
msg: "设置 msg 为新的值" + arg1 + arg2
});
}
}
copy
上方代码中,需要注意的是:当一个函数调用 bind 改变了this指向后,bind 函数调用的结果,有一个【返回值】,这个值,就是被改变this指向后的函数的引用。也就是说: bind 不会修改 原函数的 this 指向,而是改变了 “函数拷贝”的this指向。
绑定 this 并给函数传参 的方式三:箭头函数【荐】
第三种方式用得最多。
代码举例:
import React from "react";
export default class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
msg: "这是 MyComponent 组件 默认的msg"
};
}
render() {
return (
<div>
<h1>绑定This并传参</h1>
<input
type="button"
value="绑定this并传参"
onClick={() => {
this.changeMsg3("千古3", "壹号3");
}}
/>
<h3>{this.state.msg}</h3>
</div>
);
}
changeMsg3 = (arg1, arg2) => {
// console.log(this);
// 注意:这里的方式,是一个普通方法,因此,在触发的时候,这里的 this 是 undefined
this.setState({
msg: "绑定this并传参的方式3:" + arg1 + arg2
});
};
}
copy