javascript - es6箭头函数和this
问题描述
const Title=React.createClass({ getDefaults: ()=> { return{ title:’hello world’ } }, render:()=>{ return <h1>{this.props.title}</h1> } }) ReactDOM.render( <Title/>, document.getElementById(’app6’) )此种情况下报错:Cannot read property ’props’ of undefined
**请问:
(1)此种情况下箭头函数和this是否可以一起使用?(2)如果可以一起使用请问有何种解决方法?**
问题解答
回答1:可以改成
render() { console.log(this);}回答2:
万恶的ES2015!!!给你翻一下。
function template(config) { var self = this; Object.keys(config).forEach(function (key) { self[key] = config[key]; });}function factory() {}factory.create = function (config) { return new template(config);}var instance = factory.create({ title: ’instance1’, method: () => { console.log(this); }});instance.method();
function template(config) { var self = this; Object.keys(config).forEach(function (key) { self[key] = config[key]; });}function factory() {}factory.create = function (config) { return new template(config);}var instance = factory.create({ title: ’instance1’, method() { console.log(this); }});instance.method();
基础多看看,其实理解并不难
相关文章:
1. python - 求一个在def中可以实现调用本def满足特定条件continue效果的方法(标题说不太清楚,请见题内描述)2. MYSQL新建用户设置可以远程访问的问题3. $fields = $values = [];这条代码一直定义不了,一直报错,老师的源码也是被报错的,执行不了,请问该怎么解决这个问题4. java - mybatis怎么实现在数据库中有就修改,没有就添加5. node.js - nodejs和前端JavaScript 字符串处理结果不一样是什么原因?6. linux - 为什么我在mysql的my.cnf下找不到bind-address?7. 数据库设计 - MySQL数据库主键问题8. mysql同步数据到elasticsearch用什么工具?9. mysql 5.7单表300万数据,性能严重下降,如何破?10. 数据库 - mysql 远程可以连接,但是本地连接拒绝?
![$fields = $values = [];这条代码一直定义不了,一直报错,老师的源码也是被报错的,执行不了,请问该怎么解决这个问题](http://www.haobala.com/attached/image/news/202205/093622cb60.png)