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 - django 里自定義的 login 方法,如何使用 login_required()2. android-studio - Android 動態壁紙LayoutParams問題3. sql語句如何按or排序取出記錄4. angular.js - 不適用其他構建工具,怎么搭建angular1項目5. 主從備份 - 跪求mysql 高可用主從方案6. python如何不改動文件的情況下修改文件的 修改日期7. mysql優化 - mysql count(id)查詢速度如何優化?8. css3 - [CSS] 動畫效果 3D翻轉bug9. mysql主從 - 請教下mysql 主動-被動模式的雙主配置 和 主從配置在應用上有什么區別?10. node.js - node_moduls太多了
