Pair program with me! profile for carousel at Stack Overflow, Q&A for professional and enthusiast programmers

12/04/2012

The role of functions in Javascript language

Mastering functions is very important skill for Javascript developer. Compared to other composite data types ( object and array ), functions in JS have a wide application and can be used in many ways. 

So functions can:

- be called and invoke a code inside of their body
- receive arguments
- treated as object ( with properties )
- tied to a variable and passed around 
- provide scope and privacy
- act like a constructor ( and create instance of object )
- provide a framework for procedural programming

It is important to mention that JS is class-less language, and lot of function behaviour is in the goal of providing  classical paradigms and concepts. For example, function as constructor is a concept directly inherited from a classical object-oriented paradigm. In JS you don't have to use constructors, but they are present. One more example is a way that JS deals with privacy. In classical OO languages, privacy is provided through classes, and all instances share same privacy state like their parent class ( mostly ). In JS privacy is provided through functions. Concept of closures, which are functions nested inside of another functions, is a very powerfull technique that is widelly used by a lot of developers. David Flanagan.

If you wanny learn more about functions and JS in general, I would recommend Javascript patterns, which is a book written by Stoyan Stefanov - Yahoo web-developer. And if you are total novice with language, definitely read Javascript: The Definitive Guide by David Flanagan.

Happy coding.

No comments:

Post a Comment