Friday, December 23, 2011

First-class object

Is the on which can dynamically be created, destroyed at runtime during the execution of program,and pass as an argument to other functions. In some languages like javascript and python functions are also first class objects.

Ex. Passing function as an argument in Python:

          >>> def op(a,b,fun):
            return fun(a,b)

    >>> def sum(a,b):
            return a+b

    >>> op(1,2,sum)

Ex. Dynamically defined function in javascript:

var supriseMe = function () {


supriseMe = function () {
alert(" boooooooo!");
// using the self-defining function
supriseMe(); // Boo!
supriseMe(); // boooooboo!

No comments:

Post a Comment