0

I'm trying to reference the method UTCDate() from another JS file that loads after this file bootstrap-datepicker.js. Is there a way for me to access this method. I tried using just UTCDate() I even tried window.UTCDate().

Here is the original file bootstrap-datepicker.js and they seem to be calling it directly inside the datePicker itself.

(function($, undefined){

    var $window = $(window);

    function UTCDate(){
        return new Date(Date.UTC.apply(Date, arguments));
    }
    function UTCToday(){
        var today = new Date();
        return UTCDate(today.getFullYear(), today.getMonth(), today.getDate());
    }
    function alias(method){
        return function(){
            return this[method].apply(this, arguments);
        };
    }

    var DateArray = (function(){
        var extras = {
            get: function(i){
                return this.slice(i)[0];
            },
            contains: function(d){
                // Array.indexOf is not cross-browser;
                // $.inArray doesn't work with Dates
                var val = d && d.valueOf();
                for (var i=0, l=this.length; i < l; i++)
                    if (this[i].valueOf() === val)
                        return i;
                return -1;
            },
            remove: function(i){
                this.splice(i,1);
            },
            replace: function(new_array){
                if (!new_array)
                    return;
                if (!$.isArray(new_array))
                    new_array = [new_array];
                this.clear();
                this.push.apply(this, new_array);
            },
            clear: function(){
                this.length = 0;
            },
            copy: function(){
                var a = new DateArray();
                a.replace(this);
                return a;
            }
        };

        return function(){
            var a = [];
            a.push.apply(a, arguments);
            $.extend(a, extras);
            return a;
        };
    })();
me-me
  • 5,139
  • 13
  • 50
  • 91

2 Answers2

1

That is how closures work: They close over variables.

function UTCDate(){
    return new Date(Date.UTC.apply(Date, arguments));
}

is private to your IIFE. You should better switch to The Revealing Module Pattern:

var myDateApp=function(){
    ...
    function UTCDate(){
        return new Date(Date.UTC.apply(Date, arguments));
    }
    ...

    return {
            ...
            UTCDate:UTCDate,
            ...
           };
}();

Then you could easily access the function UTCDate via myDateApp.UTCDate().

Or switch later on to AMD.

Thomas Junk
  • 5,588
  • 2
  • 30
  • 43
  • Thanks Thomas for the explanation how closures work. I'm wondering how the Datepicker itself is accessing the closure then. https://github.com/eternicode/bootstrap-datepicker/blob/master/js/bootstrap-datepicker.js – me-me Aug 23 '14 at 15:25
0

You can't without modifying the code to export that method outside the anonymous closure.

plalx
  • 42,889
  • 6
  • 74
  • 90
  • If thats the case how are they calling it directly in this file. https://github.com/eternicode/bootstrap-datepicker/blob/master/js/bootstrap-datepicker.js – me-me Aug 23 '14 at 15:16
  • @ericb which line is it called outside the closure? – Thomas Junk Aug 23 '14 at 15:25
  • I believe 758, 779, 933 and multiple other areas. – me-me Aug 23 '14 at 15:27
  • @ericb The whole code you referenced all share the same top-level closure in which `UTCDate` is defined. Look carefully. – plalx Aug 23 '14 at 15:40
  • Ah you are right. So I guess there is no way to be able to get to it without altering the datepicker file itself. – me-me Aug 23 '14 at 15:45