rip*_*234 66 javascript design-patterns optional-parameters option
// opt_options is optional
function foo(a, b, opt_options) {
// opt_c, opt_d, and opt_e are read from 'opt_options', only c and d have defaults
var opt_c = 'default_for_c';
var opt_d = 'default_for_d';
var opt_e; // e has no default
if (opt_options) {
opt_c = opt_options.c || opt_c;
opt_d = opt_options.d || opt_d;
opt_e = opt_options.e;
}
}
Run Code Online (Sandbox Code Playgroud)
以上看起来非常冗长.使用默认参数处理参数选项的更好方法是什么?
max*_*max 72
这使用jQuery.extend但可以与您选择的库中的对象合并或ES6中的Object.assign互换.
function Module(options){
var defaults = {
color: 'red'
};
var actual = $.extend({}, defaults, options || {});
console.info( actual.color );
}
var a = new Module();
// Red
var b = new Module( { color: 'blue' } );
// Blue
Run Code Online (Sandbox Code Playgroud)
编辑:现在也在underscore或lodash!
function Module(options){
var actual = _.defaults(options || {}, {
color: 'red'
});
console.info( actual.color );
}
var a = new Module();
// Red
var b = new Module( { color: 'blue' } );
// Blue
Run Code Online (Sandbox Code Playgroud)
在Javascript ES6中,您可以使用Object.assign:
function Module(options = {}){
let defaults = {
color: 'red'
};
let actual = Object.assign({}, defaults, options);
console.info( actual.color );
}
Run Code Online (Sandbox Code Playgroud)
wpr*_*prl 34
ES6/ES2015有几种新方法.使用Object.assign:
options = Object.assign({}, defaults, options);
Run Code Online (Sandbox Code Playgroud)
使用解构赋值:
const { a = 1, b = 2 } = options;
Run Code Online (Sandbox Code Playgroud)
您还可以使用解构函数参数:
const ƒ = ({a = 1, b = 2, c = 3} = {}) => {
console.log({ a, b, c });
};
Run Code Online (Sandbox Code Playgroud)
没有依赖!
Ric*_*ard 20
要获得没有其他依赖项的默认选项,我使用以下模式:
var my_function = function (arg1, arg2, options) {
options = options || {};
options.opt_a = options.hasOwnProperty('opt_a') ? options.opt_a : 'default_opt_a';
options.opt_b = options.hasOwnProperty('opt_b') ? options.opt_b : 'default_opt_b';
options.opt_c = options.hasOwnProperty('opt_c') ? options.opt_c : 'default_opt_b';
// perform operation using options.opt_a, options.opt_b, etc.
};
Run Code Online (Sandbox Code Playgroud)
虽然有点冗长,但我发现它易于阅读,添加/删除选项并添加默认值.当有很多选项时,稍微紧凑的版本是:
var my_function = function (arg1, arg2, options) {
var default_options = {
opt_a: 'default_opt_a',
opt_b: 'default_opt_b',
opt_c: 'default_opt_c'};
options = options || {};
for (var opt in default_options)
if (default_options.hasOwnProperty(opt) && !options.hasOwnProperty(opt))
options[opt] = default_options[opt];
// perform operation using options.opt_a, options.opt_b, etc.
};
Run Code Online (Sandbox Code Playgroud)
imb*_*olc 11
更紧凑的jQuery版本:
function func(opts) {
opts = $.extend({
a: 1,
b: 2
}, opts);
console.log(opts);
}
func(); // Object {a: 1, b: 2}
func({b: 'new'}); // Object {a: 1, b: "new"}
Run Code Online (Sandbox Code Playgroud)
rip*_*234 -6
现在想起来,我有点喜欢这样:
function foo(a, b, opt_options) {
// Force opt_options to be an object
opt_options = opt_options || {};
// opt_c, opt_d, and opt_e are read from 'opt_options', only c and d have defaults
var opt_c = 'default_for_c' || opt_options.c;
var opt_d = 'default_for_d' || opt_options.d;
var opt_e = opt_options.e; // e has no default
}
Run Code Online (Sandbox Code Playgroud)