When creating JavaScript factory functions I was taught that the return syntax looks like the below:
function FF(constructorArg) {
var _privateName = constructorArg;
var publicMessage = "Hello StackOverflow";
function publicMethodGetName() {
return _privateName;
}
return {
publicMethodGetName: publicMethodGetName,
publicMessage: publicMessage
};
}
However, I've just noticed that I can simplify the return to the following:
return { publicMethodGetName, publicMessage };
and it still works.
Are there any unintended side effects of using this simpler syntax or is it fine to use?
That's a shorthand syntax of defining object literal properties that was introduced by ECMAScript2015 (a.k.a. ES6). The only possible problem is environmental support. The environment should support this feature.
For supporting ES5-based environments you can use an ES6 transpiler and convert the code into ES5.