|
|
|
|
|
by reissbaker
4113 days ago
|
|
Yep, they're explicitly trying to do that. For what it's worth, you can still use "options hashes" in your argument APIs in strong mode using objects; you just write a library function something like: function options(options, defaults) {
// the final args start off as a clone of the default args
let args = Object.clone(defaults);
// we then loop through the keys and copy in any overrides
for(let key of Object.keys(args)) {
// ignore inherited properties and skip missing ones
if(args.hasOwnProperty(key) && options.hasOwnProperty(key)) {
args[key] = options[key];
}
}
// args now has all of the overrides from options
return args;
}
And then in all your functions that take options objects: function bakeBread(ingredientOverrides) {
let ingredients = options(ingredientOverrides, {
flourType: 'whole wheat',
sugarAmount: '3 tbsp',
waterAmount: '1 cup',
milkAmount: '0.3 cups',
flourAmount: '4 cups'
});
let batter = mix(ingredients);
return bake(batter);
}
JS is still quite dynamic, even in strong mode — you can define arbitrary objects and types at runtime, and easily inspect/reflect on them — it's just a little harder to silently corrupt data.The neat thing about using named arguments with objects is that in typed variants of JS — for example, TypeScript, or perhaps someday SoundScript — you can actually typecheck them! Maps can't do that in any language I know of: by design they can contain anything. |
|