[UPDATE]: It turns out there are tow reason for my question:
For CAN NOT: It is my lack of knowledge about how arrow function works, it just locks the surrounding context everywhere it defined. And the global is undefined
in strict mode.
For CAN: It could be a bug of old version Chrome Dev Tool.
Please refer to comment to find out what are the CAN and CAN NOT here
All:
I am pretty new to ES6, when I try arrow function's concept of lexical context locking, I use code like:
var glb = {name: "glb" };
glb.showname = () => {
/******* interesting thing happens here below *******/
console.log(this);
this.name = "hello"
return () => {
console.log(this.name);
}
}
const ext = {name: "ext"};
ext.showname = glb.showname();
ext.showname();
And I transpile it with:
browserify main.js -o bundle.js -t [ babelify --presets [ es2015 ] ] -d
Interesting thing is:
When I run it without setting breaking point(either with Node or in Chrome), it always give me error:
Uncaught TypeError: Cannot set property 'name' of undefined
and the console just prints out undefined
But when I set a break point(I do it in Chrome), this time, the context can be printed out like:
Object {name: "glb"}
Any idea why this happens?
Thanks