When I was trying to write some unit tests for ECMAScript6 class, I encoutered following problem: constructor is not being called during imported class instantiation.
I quickly found out that it is some behaviour of Babel + node.js I just can't understand. Here is the smallest piece of code showing the problem:
// dummy.js
export class Dummy {
construct(){
this.value = 1
}
set(value){
this.value = value
}
}
// main.js
require("babel-register")({
presets: [
"es2015"
],
});
var Dummy = require('./dummy.js').Dummy
var d = new Dummy()
console.log(d.value) // undefined, should be 1
d.set(13)
console.log(d.value) // 13
It really confuses me, because method set()
works as expected. Originally I got such weird behaviour with ES6 import
statement, however I couldn't use it here as node.js does not support it. As You can see we get the same problem with require
.
I use node v6.10 and babel v6.24.
via reidar13
No comments:
Post a Comment