Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

frameworkname({
//framework code here
});

so my framework code doesn't conflict with other frameworks. I know that frameworkname({}); 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:$(document).ready(function(){//codehere});), but how did the jquery developers do this? I want to be able to do this for my framework. Any help is greatly appreciated.

like image 294
Kevin Pei Avatar asked Sep 11 '11 15:09

Kevin Pei


1 Answers

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(); }
bar(foo);
like image 71
Quentin Avatar answered Oct 16 '22 19:10

Quentin