简体   繁体   中英

assigning function to variables and assigning variables to functions(?) in Javascript

I'm learning javascript by dissecting a few scripts written by other people that I have used before (stackoverflow is essential to this!). This bit of code (and I cut a lot from the middles to make the code easier to read, and all comments are mine) comes from fullCalendar.js , a jquery plugin.

I'm not sure what the author is doing here:

function EventManager(options, _sources) { //called using .call()
    var t = this;

    t.isFetchNeeded = isFetchNeeded;  //?? assign the function "isFetchNeeded" to the variable (not the value returned by the function)??

    var trigger = t.trigger;    //?? namespace??
}

function View(element, calendar, viewName) {
    var t = this;   

    function trigger(name, thisObj) {
        return calendar.trigger.apply(
                 calendar,
                 [name, thisObj || t].concat(Array.prototype.slice.call(arguments, 2), [t])
        );
    }
}

First, my guess is that this.foo=bar is assigning the function "bar" to the variable called "isFetchNeeded" within the current object (not yet sure why you'd do this. speed?).

Second, the line var foo=this.bar has me stumped. It looks like it might be some namespace magic.

Am I correct in thinking about the first line? and what's the next line doing?

Thanks

this.foo = bar assigns the value of bar (whatever it may be) to the object's member named foo . This can indeed be a function definition along with a host of other things.

What var trigger = t.trigger does is declare a variable named trigger in the scope of the EventManager function. There are other functions defined in a lower scope, namely pushLoading and popLoading . These are not object members, so using this.trigger inside of those functions would not work. The var trigger declaration makes it accessible in these functions.

I don't claim to be anywhere close to the best JS programmer ever, so I can't say whether what he's doing makes a ton of sense or not, but you can see how EventManager.js is actually used (in Calendar.js ):

EventManager.call(t, options, eventSources);
var isFetchNeeded = t.isFetchNeeded;
var fetchEvents = t.fetchEvents;

He does not actually create an EventManager object, but he calls its constructor with the Calendar object. Calender does have trigger defined, which is why var trigger = t.trigger works inside of EventManager . Similarly, the line

var isFetchNeeded = t.isFetchNeeded;

In Calendar.js acquires the isFetchNeeded method "back out of" EventManager and allows it to be called in the Calendar scope.

The variable this references the context the function was called in. In this case (assuming the code is being run in a web browser) is the window object. The first line simply assigns the function isFetchNeeded to be a method of the this object. The second line assigns the value of this 's member variable trigger to a variable local to the function EventManager .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM