I was exploring ES6 const keyword, everything is working for me, except some scoping issue which I am unable to understand.
I have a a simple js file
const reducer = (state = 0,action) => {
switch(action.type) {
case 'Increment' :
return state+=1;
case 'Decrement' :
return state-=1;
default :
return state;
};
}
const {createStore} = Redux;
const store = createStore(reducer);
const getstate = () => {
return store.getState();
}
// var getstate = () => {
// return store.getState();
//} it is working
store.subscribe(() => {
console.log(this);
console.log(this.getstate());
//console.log(getstate()); it is working
})
store.dispatch({type:'Increment'});
In my subscriber method this.getstate is returning myjs.js:20 Uncaught TypeError: this.getstate is not a function
when I change const getstate
to var getstate
, it started working . Also when I changed this.getstate
to getstate
, it is also working
From browser console I can access getstate but I am unable to access this.getstate. this here is in Window scope, but then why it is unable to access const variable?