Read the statement by Michael Teeuw here.
Syntax Style for node_helper.js
-
I’m looking at a lot of modules while trying to roll my own and see a lot of differences in syntax style, I wonder if one way of creating a function is better or more efficient than the other.
init: function () and init()
I see both ways done in node_helper.js in different modules.
module.exports = NodeHelper.create({ init: function () { console.log ("init module helper "); } })
and
module.exports = NodeHelper.create({ init () { console.log ("init module helper "); } });
-
@kayakbabe doesnt matter, only processed once at page load
older versions of nodejs only supported type 1
-
@kayakbabe said in Syntax Style for node_helper.js:
I see both ways done in node_helper.js in different modules.
The short variant is known as “Shorthand Method Names” and has been available since 2015 (with the release of ECMAScript 6).
-
@KristjanESPERANTO abd @sdetweil
for MagicMirror purposes, is there a preferred style to be used for modules?
-
@kayakbabe no… up to you
-
for MagicMirror purposes, is there a preferred style to be used for modules?
@kayakbabe I agree with Sam, but here’s a more detailed answer anyway: This depends on where you are. In some modules there are no rules for this - there and in your own modules it is, as Sam says, up to you.
However, to avoid poor code style, such as spaghetti code and inconsistently formatted code, which not only make maintenance and further development more difficult, but also affect team collaboration, many projects use static code analysis tools such as prettier and ESLint for checking code quality.
In the MagicMirror core (and with this in the default modules), we use the ESLint
object-shorthand
rule (see https://archive.eslint.org/docs/rules/object-shorthand) to ensure that only the shorthand version is used.