Kevin Pei Kevin Pei - 1 year ago 61
Javascript Question

Pass code as function argument in javascript?

I'm trying to create a javascript framework by myself(so no jquery,mootools... code please) and I want to make the code for my framework only accessible in a framework function so for example, something like this:

//framework code here

so my framework code doesn't conflict with other frameworks. I know that
is a function, but I don't know how you pass code as a function argument. I know this is possible as I'm quite experienced in jquery and jquery has that stuff everywhere (example:
), but how did the jquery developers do this? I want to be able to do this for my framework. Any help is greatly appreciated.

Answer Source

Functions are first class objects in JavaScript. You can pass them around in the same way as Strings, Numbers, Arrays or any other data type.

var foo = function () { alert(1); };
var bar = function (arg) { arg(); }