es6 arrow functions and this
迷茫
迷茫 2017-07-05 10:37:12
0
2
715
 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

**Excuse me:

(1)此种情况下箭头函数和this是否可以一起使用?
(2)如果可以一起使用请问有何种解决方法?**
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
巴扎黑

can be changed to

render() {
    console.log(this);
}
Ty80

The evil ES2015!!!
Let me turn it over for you.

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();

Read more about the basics, it’s actually not difficult to understand

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template