Problem: A Javascript function needs few parameters to work with:
function kick(person, reason, amount) {
// kick the *person* with the *amount*, based on the *reason*
}
As there's no way to do function overloading in JS like how you do in Java, if it needs to be designed for easy future improvement (parameters adding), it can be written as:
/* Function Parameters pattern */
function kick() {
// kick the person as in *arguments[0]*, with the amount as in *arguments[1]*,
// based on the reason as in *arguments[2]*, with the strength as in *arguments[3]*
}
or
/* Object Configuration Pattern */
function kick(config) {
// kick the person as in *config.person*, with the amount as in *config.amount*,
// based on the reason as in *config.reason*, with the strength as in *config.strength*
}
I do know that Object Configuration Pattern allows augmentation for any default properties.
So, the question is: If I don't need to augment any properties with the parameters, is there any significant reason of using any one of the proposed solutions as opposed to the other?
What is a Pattern? A pattern is a reusable solution that can be applied to commonly occurring problems in software design - in our case - in writing JavaScript web applications. Another way of looking at patterns are as templates for how we solve problems - ones which can be used in quite a few different situations.
The Prototype pattern allows you to create an object using another object as a blueprint, inheriting its properties and methods. If you've been around JavaScript for a while, you're probably familiar with prototypal inheritance and how JavaScript works around it.
Description. Config. js allows developers to configure their applications in an XML block instead of hard-coding values inside their scripts or in JSON objects. The XML can be embedded inside an HTML document or in a separate XML file. The configuration block may contain strings, numbers, arrays and HTML.
The factory pattern is a creational design pattern that provides a generic interface for creating objects. In the factory pattern, we can specify the type of object being created and we do not need to explicitly require a constructor.
Using an object has a few advantages:
Consider the following two calls:
kick({user: u,
reason: "flood",
log: true,
rejoin: false,
timeout: 60000,
privmessage: true});
kick(u, "flood", true, false, 60000, true);
and imagine someone else reading the call. What is the first true
? Note also that yourself in a few month would be in the same exact position (not remembering what is the fourth parameter to kick
is very similar to not knowing it).
With the object approach you can pass a function a set of parameters that this function must use to call another function
function kickgroup(users, parms) {
for (var i=0; i<users.lenght; i++) {
var uparms = Object.create(parms);
uparms.user = users[i];
kick(uparms);
}
}
Note also that in the arguments
case you don't need to punish yourself by using arguments[x]
syntax. You can just declare the parameters and add them as the function evolves: any parameter that has not been passed will be set to undefined
(and if you need you can still access arguments.length
to distinguish if the caller explicitly passed your function undefined
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With