Wednesday, December 21, 2011

Classes in Javascript

Source : >http://www.phpied.com/3-ways-to-define-a-javascript-class/

3 ways to define a JavaScript class


Introduction


JavaScript is a very flexible object-oriented language when it comes to syntax. In this article you can find three ways of defining and instantiating an object. Even if you have already picked your favorite way of doing it, it helps to know some alternatives in order to read other people's code.

It's important to note that there are no classes in JavaScript. Functions can be used to somewhat simulate classes, but in general JavaScript is a class-less language. Everything is an object. And when it comes to inheritance, objects inherit from objects, not classes from classes as in the "class"-ical languages.


1. Using a function


2. Using object literals


3. Singleton using a function


You saw three (plus one) ways of creating objects in JavaScript. Remember that (despite the article's title) there's no such thing as a class in JavaScript. Looking forward to start coding using the new knowledge? Happy JavaScript-ing!

No comments:

Post a Comment