Contraindications in Iife’s

Asked

Viewed 24 times

2

Is there any contraindication using IIFE’s within the context of an object?

For example, in the properties begin and created i do the auto run function to set the properties at the time of variable instantiation.

Example:

var example = {
                example_id: scope.id,
                //Formating date to US
                begin: (function () {
                    var dateBr = scope.begin;
                    var dateUs = dateBr.split('/').reverse();
                    return dateUs.join('-');
                }()),
                period: scope.period,
                created: (function () {
                    var d = new Date();
                    var curr_day = d.getDate();
                    var curr_month = d.getMonth() + 1; //Months are zero based
                    var curr_year = d.getFullYear();
                    var curr_hour = d.getHours();
                    var curr_min = d.getMinutes();
                    var curr_sec = d.getSeconds();
                    return curr_year + "-" + curr_month + "-" + curr_day + " " + curr_hour + ":" + curr_min + ":" + curr_sec;
                }())
            };

I would like to know if using the IIFE so can bring some problem?

1 answer

3


No, I see no problem in doing so. On the contrary, there are advantages, since you are not leaking variables to the global scope.

  • Thanks Friend, I already confirm the answer face, because I know you a long time here from the OS and it is not the first time that helps me. Hugção.

  • 1

    You’re welcome. I may be wrong too, but I’ve never really seen any contraindications to this practice.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.