Define a JavaScript function with 1 parameter, then call it without parameters

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?

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();
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Define a JavaScript function with 1 parameter, then call it without parameters

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录