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. mysql優化 - mysql 一張表如果不能確保字段列長度一致,是不是就不需要用到char。2. mysql 怎么做到update只更新一行數據?3. 使用python中的pandas求每個值占該列的比例4. javascript - 新浪微博網頁版的字數限制是怎么做的5. python - scrapy 如何組合2個不同頁面的數據,一并存儲6. python2.7 - python 函數或者類 代碼的執行順序7. sublime可以用其他編譯器替換嗎?8. javascript - 用jsonp抓取qq音樂總是說回調函數沒有定義9. python - 多態調用方法時卻顯示bound method...10. node.js - mysql如何通過knex查詢今天和七天內的匯總數據
