I'm using Backdraft loader which follows CommonJS standard. I'm trying to understand how everything works but now I need some advice.
Let's say I want to get a timestamp and I do this:
define(function(){
return (new Date()).getTime()
});
Then I do a simple require and try to开发者_JS百科 run it:
require(['timestamp'],function(timestamp) {
console.log(timestamp())
console.log(timestamp())
console.log(timestamp())
}
Now what I get is the same timestamp 3 times from the point when timestamp function was evaluated. Does define() do a closure automatically or what's going on?
I'm learning functional programming so not understanding how to do these small functions that can be reused is kinda hampering my efforts :)
It's not about Functionnal programming. It's about the CJS module definition.
In the above example, after you require "timestamp" module, Backdraft loader(Suppose it works the same with RequireJS since I don't know Backdraft) will help to new timestamp and cache it. So each time you call that function. It returns the same value.
精彩评论