0

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?

2

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.