Reputation: 17785
This thread describes how you can use Javascript obect literal notation to describe a collection of functions for example:
var crudActions = {
create : function () {
...
}
read : function () {
...
}
}
Is there a name for this pattern? And is there an advantage to using this approach?
Upvotes: 2
Views: 4472
Reputation: 1380
create the object and define the property and value
var personA = {
name:"personName",
age : 23,
sex : "Male",
info:function() {
console.log(name + ": "+age+ ": "+sex);
}
};
declaration method of variable and function in object. This method mostly used in closure.
Upvotes: -2
Reputation: 119847
what you just did is give your functions a "namespace". your functions are now a "collection of related tasks"
namespacing means that is your functions don't "live" in the global scope anymore (and thus avoid polluting/overwriting it with other functions). so all your functions can be addressed from the "namespace" and not worry if another function has the same name as it (like another create()
).
like say in your app you have a database and a view. both can do a "create" but having 2 create()
functions is not possible. creating weird names like createDatabase()
and createView()
is just not organized. thus you create namespaces so they can be called database.create()
and view.create()
- makes more sense.
Upvotes: 5