Holy Diver
Holy Diver

Reputation: 41

Defining a JavaScript function with 1 parameter, then calling it without any paramters

I was looking at the following piece of code as an example use of Google's JS Youtube API:

    function outputStatus(e) {
        alert("e.success = " + e.success +"\ne.id = "+ e.id +"\ne.ref = "+ e.ref);
    }
    swfobject.embedSWF("test6.swf", "myContent", "300", "120", "9.0.0", "expressInstall.swf", null, null, null, outputStatus);

Page it's from: http://www.bobbyvandersluis.com/swfobject/testsuite_2_2/test_dynamic2.html

API documentation: https://code.google.com/p/swfobject/wiki/api#swfobject.embedSWF%28swfUrlStr,_replaceElemIdStr,_widthStr,_height

So I'm trying to figure out how this works. I understand that outputStatus(e) is being used as a callback function for swfobject.embedSWF(...), but I don't understand how its getting called without a parameter. Can someone explain the machinery that goes into a procedure like this?

Upvotes: 2

Views: 114

Answers (4)

hugomg
hugomg

Reputation: 69934

Functions in Javascript can be passed around and assigned to variables and function parameters:

function myFunction(x){
    console.log("Hello", x);
}

var func = myFunction; //This just makes "func" refer to myFunction;
                       //It does NOT call "myFunction" yet because there are no parenthesis.

func(10);

Passing functions as parameters to other functions works similarly:

function myFunction(x){
    console.log("Hello", x);
}

function doIt(func){
    func(10);
}

doIt(myFunction);

Upvotes: 0

polyslush
polyslush

Reputation: 111

As others have mentioned, only the callback for the function is being passed. outputStatus is not being explicitly called in the code example you provide - embedSWF is presumably calling outputStatus internally.

JavaScript functions can be called without parameters, however, and they can also be called with more parameters than are defined on the function. This behavior can be useful since function overloading is not built in to JavaScript. In this example, however, calling outputStatus without a parameter would result in e being undefined, and an exception would be thrown.

Upvotes: 0

Andrei Nemes
Andrei Nemes

Reputation: 3122

It's not being called without a parameter. What you did here

swfobject.embedSWF("test6.swf", "myContent", "300", "120", "9.0.0", "expressInstall.swf", null, null, null, outputStatus);

is pass the function reference to your embedSWF function, not execute it.

The embedSWF function is calling that function independently as a callback somewhere in its code.

To understand how this works, you have to understand that functions are first class citizens in JavaScript. That means they can be stored in variables and passed along as parameters. Consider the following code

var myFunction = function() {
    //do something
}

//storing the function reference in another variable
//in a sense, 'pointing' at it
//notice the absence of parantheses (), which would invoke the function immediately
var callback = myFunction;

//execute the function pointed at by callback
//which is the same as executing myFunction
callback();

Upvotes: 4

Tyler McGinnis
Tyler McGinnis

Reputation: 35276

You have to remember that somewhere, swfobject.embedSWF is defined as a function. Let's pretend it looks like this.

swfobject.embedSWF = function(){

}

Now when we pass in the outputStatus function as a callback, embedSWF can do something like this

   swfobject.embedSWF = function(outputStatus){
     var e = 'something useful';
     outputStatus(e);
   }

So it's eventually getting called with a parameter of e, but they have encapsulated that away from you as the user of their API.

Upvotes: 2

Related Questions