Exploring the functionalities of Babel and ES6, I found myself stuck while transpiling some code at this particular section:
class App extends SomeParent {
myFunction() {
}
}
I am curious about the output that caught my attention:
var _createClass = function() {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function(Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
My question revolves around the usage of _createClass through an Immediately Invoked Function expression (IIF) followed by returning another function. Wouldn't it be more straightforward to implement something like this instead:
var _createClass = function (Constructor, protoProps, staticProps) {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
}
Is there a specific reason or best practice behind using the IIF approach and returning another function?
For those interested, you can access a Babel demo here