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. 零基础的新手小白想学PHP开发应该从哪儿开始学起来2. mysql 查询身份证号字段值有效的数据3. web服务器 - ubuntu下布置apache加wsgi加python4. 搜索出来的值怎样在前台显示不出来,5. win10 python3.5 matplotlib使用报错6. js得到了我服务器上word文档的路劲,怎么展示在新的页面上7. python的jinja中中文问题8. mysql - SQL添加记录的数据来源于同一个表9. python+jinja2编写前端界面,发生Unexpected end of template问题,求解决!10. navicat - Mysql无法连接

网公网安备