Declaring a function map using "|| {}" in JavaScript
I saw the following syntax in JavaScript that allows you to add functions
to an Objects element so you can perform a switch alternative. Consider
the following:
var insert = insert || {};
insert.Actor = function (user) {
//Do Somthing
}
This would allow you to do the following:
function addUser(type) {
if (insert[type]) {
return insert[type](user);
}
}
I like this implementation but I have two questions:
What exactly is this statement doing and could i just declare a regular
object?
var insert = insert || {};
Besides readability what advantages do I get by using this instead of a
regular switch statement.
No comments:
Post a Comment