A function can behave like any other object in javascript. It can be assigned to a variable, popped into an array, or stuffed in an object:
PHP Code:
obj['add'] = function(x, y) { return x + y; }
var sum = obj.add(1, 2);
var other_sum = obj['add'](1, 2);
At the console in Chrome (or Safari):
Code:
> var obj = {};
undefined
> obj['add'] = function(x, y) { return x + y; }
function (x, y) { return x + y; }
> obj.add;
function (x, y) { return x + y; }
> obj['add'];
function (x, y) { return x + y; }
> obj.add(1, 2);
3
> obj['add'](3, 4);
7
Bookmarks