Today I saw a new syntax JS classes could assign their properties, like this:
// Syntax 1
class foo {
constructor(prop){
this.prop = prop;
}
}
// Syntax 2
class bar {
prop = 5;
}
let testBar = new foo(5);
let testFoo = new bar();
console.log(testBar.prop);
console.log(testFoo.prop);
Are their any differences between the first and second syntaxis besides that the second syntax isn't dynamic (5 is hardcoded now)? Is the second syntax being transformed under the hood to the first syntax or do they have different characteristics?
Also a good source on this topic would be appreciated since I could not find anything on this specific topic.
The second one is no valid ES yet, but there is a proposal so that might work in the future. If you use a transpiler like Babel to use it today, then the second will be transpiled to the first, so yes, they are equal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With