You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Mar 25, 2018. It is now read-only.
A common pattern probably in any node programming style, is to pass options. Contrary to, say golang, it's rather common to pass some let options = {}. Especially on OOP js I assume this is heavily used for class instance configurations.
However, using plain js can sort of make a quick end to this approach, when used with nested objects.
Even though this might be a small guide and pattern this could lead to better style of the community if we'd advise for a solution. Object.assign(defaultObj, [...objs]) could now be nicely used for this. See examples (old and new below):
'use strict'functionoldApi(options){letopts=options||{}opts.name=options.name||'the default name'opts.test.count=options.test.count||1returnopts}lettestOptions_1={name: 'better name',test: {count: 2}}console.log(oldApi(testOptions_1));// let testOptions_fail = {// name: 'better name'// }// console.log(oldApi(testOptions_fail)); this will fail ->// TypeError: Cannot read property 'count' of undefinedfunctionapi(options){constsomeDefault={name: 'the default name',test: {count: 1}}letopts=Object.assign(someDefault,options)returnopts}lettestOptions_2={name: 'better name',test: {count: 2}}console.log(api(testOptions_2));// let testOptions_success = {// name: 'better name'// }// console.log(api(testOptions_success)); // this will not fail
The text was updated successfully, but these errors were encountered:
Closing as this repository is dormant and likely to be archived soon. If this is still an issue, feel free to open it as an issue on the main node repository.
A common pattern probably in any
node
programming style, is to pass options. Contrary to, saygolang
, it's rather common to pass somelet options = {}
. Especially on OOPjs
I assume this is heavily used for class instance configurations.However, using plain
js
can sort of make a quick end to this approach, when used with nested objects.Even though this might be a small guide and pattern this could lead to better style of the community if we'd advise for a solution.
Object.assign(defaultObj, [...objs])
could now be nicely used for this. See examples (old and new below):The text was updated successfully, but these errors were encountered: