Reputation: 6781
I'm reading a piece of code that is built like this:
var Template = function() { return `<?xml version="1.0" encoding="UTF-8" ?>
<document>
//other stuff
</document>`
}
The function is used in another file by doing
var resource = Template.call(self);
Coming from a C++/Objective-C/Swift background, I am guessing it's a function named Template
and returns whatever the stuff inside is. Could someone advise me what this construct is supposed to be?
Upvotes: 0
Views: 50
Reputation: 11106
Within the body of a script,
var x = function() { };
is equivalent to
function x() { }
as the first is a variable declaration with a function body assigned, it should be terminated by a ;
other than a function definition.
The reason doing something like this is, that the variable scope applies. Having this inside a function
function a() {
var x = function () { ... };
}
means, that function x
isn't defined outside of a
; calling x
outside of a
results in a reference error.
Upvotes: 3