2

What is the last line of code called? I know there is a Javascript variable saveFile being used as a function, passing the parameter "file", but I've never seen the last line of code used before.

var saveFile = function (file) {

    // do the stuff   

} (typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content);
4
  • @JohnColeman This particular idiom in JavaScript programming does have a name, and it's called a "self-executing anonymous function". Commented Oct 25, 2019 at 22:04
  • @Dai -- but you are making a guess as to what OP means by "this". For all anyone knows, perhaps they are confused by typeof or the use of the logical operators. Commented Oct 25, 2019 at 22:04
  • @JohnColeman The OP wrote "What is the last line of code called?" - I assume they're asking about the } ( stuff ); syntax which is a hallmark of a SEAF. Commented Oct 25, 2019 at 22:06
  • @Dai You are probably right. (+1) for a good answer. Commented Oct 25, 2019 at 22:07

2 Answers 2

3

It's a self-executing anonymous function - with a return value.

I know there is a Javascript variable saveFile being used as a function, passing the parameter "file", but I've never seen the last line of code used before.

This is not what is happening. saveFile is not the function - it's a variable that's assigned a value that is returned from that function.

Here's what the engine does when it runs that code:

  1. Define the anonymous function.
  2. The anonymous function is immediately followed by parenthesis which means the anonymous function should be invoked immediately.
  3. The expression typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content is evaluated and the resulting value is used as the argument for the file parameter for the anonymous function.
  4. The anonymous function runs its code and returns a value.
  5. That return value is then stored in saveFile.

It's equivalent to doing this:

function anonymous_function( file ) {
    // do the stuff
}

var fileArg = typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content;

var saveFile = anonymous_function( fileArg );
Sign up to request clarification or add additional context in comments.

2 Comments

Great answer Dai!, just one comment I believe anonymous_function is a named function (A function declaration).
@ricardoorellana In OP's posted code, it's an anoynmous function. I posted code to show equivalent code if the function was a named function to make it easier to understand what's going on.
2

Let's break this into multiple steps:

  1. You are creating a function expression and assigning the value to a variable called saveFile your function defines one parameter file:

    var saveFile = function (file) {}

  2. Your function gets executed immediately after creation and you pass an argument:

    var saveFile = function (file) { // do the stuff } (typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content);

  3. Your argument value depends on a short circuit evaluation:

    (typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content);

Edit: Remember this, functions are first class objects in JavaScript, this simple means that you can do things like passing functions as arguments, declaring a function an assign it to a variable, add functions as values of objects, arrays and even return a function as a value.

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.