230 likes | 723 Views
Advanced javascript. Functional programming. Concept.
E N D
Advanced javascript Functional programming
Concept Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast with the imperative programming style that emphasizes changes in state.
Concept Higher-order function • take one or more functions as an input • output a function
Anonymous Fuction • var sum = function(x,y,z) • { • return (x+y+z); • }; • sum(1,2,3); function sum(x,y,z) { return (x+y+z); } ; Sum(1,2,3)
Using function as an expression alert (“Hello, World!"); • (alert) (“Hello, World!"); • ( function(x,y,z) { return (x+y+z) } ) (1, 2, 3);
Passing function as a parameter and applying it varpassFunAndApply = function (fn,x,y,z) { return fn(x,y,z); }; varsum = function(x,y,z) { return x+y+z; }; alert( passFunAndApply(sum,3,4,5) ); // 12
functional concepts • Functions need not have names all the time. • Functions can be assigned to variables like other values. • A function expression cqn be written and enclosed in parentheses for application later. • Functions can be passed as arguments to other functions.
Resource http://www.ibm.com/developerworks/java/library/wa-javascript.html