You may want to write functions that take many arguments, some of
which are optional.
function drawElement( color, border, width, height,
left, top, zIndex) {
// Make and display an element with these variables
}
drawElement("red", 4, null, null, 100, 10);
Function signatures like this aren’t ideal for a couple of reasons:
■ It’s hard to remember the exact order of arguments.
■ You have to specify null values when you want to use the default
value for arguments in the middle of the signature.
■ Did you notice that I forgot to specify a value for zIndex? It’s hard to
count all those arguments correctly!
Passing multiple values in a single object is often a better solution:
function drawElement(options) {
// Make and display an element with the values in options
}
drawElement({
color: "red",
border: 4,
left: 100,
top: 10
});
Specifying default values is a little bit trickier with this technique. You’ll
need to create an object holding all the defaults and merge it with the
options object.
which are optional.
function drawElement( color, border, width, height,
left, top, zIndex) {
// Make and display an element with these variables
}
drawElement("red", 4, null, null, 100, 10);
Function signatures like this aren’t ideal for a couple of reasons:
■ It’s hard to remember the exact order of arguments.
■ You have to specify null values when you want to use the default
value for arguments in the middle of the signature.
■ Did you notice that I forgot to specify a value for zIndex? It’s hard to
count all those arguments correctly!
Passing multiple values in a single object is often a better solution:
function drawElement(options) {
// Make and display an element with the values in options
}
drawElement({
color: "red",
border: 4,
left: 100,
top: 10
});
Specifying default values is a little bit trickier with this technique. You’ll
need to create an object holding all the defaults and merge it with the
options object.