views:

146

answers:

4

I recently found out that Javascript function can have classes, so I was wondering if OOP is also possible through javascript. Is It? If yes, Could you please point out some tutorials or site, where I can start with?

+5  A: 

OOP is definitely possible. While Javascript doesn't have "classes" like most OO languages do, what it does have is called "prototypes". Basically, objects are defined in terms of other objects rather than classes. (Objects can also emulate classes to some degree, for those who can't wrap their minds around prototypal inheritance.)

One could argue JS's OO capabilities exceed those of most languages, considering objects play an even more essential role than in languages with classes.

cHao
replace "most OO languages" with "big budget OO languages"
Javier
+2  A: 

OOP is central to Javascript, but it's not classical OOP. Javascript uses prototypes, not classes.

Douglas Crockford is a Javascript genius, so his Prototypal Inheritance in Javascript is a nice starting point. A Google search for "Javascript OOP" likely will turn up some neat articles to peruse, as well — I like the article by Mike Koss.

Matchu
Crockford is also a Javascript Nazi. Don't take everything he says as gospel.
MooGoo
+1  A: 

Javascript is an intrinsically OOP language. Like others have said, it is classless, but you have a choice of how you want to create objects.

You can create objects that make use of different types of inheritance.

  1. A pseudo-classical inheritance. Here you build constructor functions and use new to create classes. This will look most like the typical class based OOP.
  2. Prototypal inheritance. - This is what many of the other answer referred to.
  3. Functional inheritance. - In this mode you make use of closures, anonymous functions, and strategic returns to create truly private and protected variables.

There's a fair amount of cross over among these types. Suffice it to say that Javascript is a very flexible and powerful language for OOP.

I'm just learning about OOP in JS as well. Here is an example of functional inheritance I put together:

jsFiddle

// Object constructor
var parent = function (initial) {
    var that, privateNumber, hiddenVar; 
    that = {};    

    // Public variables
    that.share = initial - 32;

    // Public methods
    that.getNumber = function () {
        return privateNumber;                
    };

    // Private properties
    privateNumber = initial; 
    hiddenVar = "haha can't get me";    

    return that;
};

// Second object constructor that inherits from parent
var child = function (initial) {
    var that, privateName;

    // Inherit from parent
    that = parent(initial);

    // Public methods
    that.getName = function () {
        return privateName;                
    };

    // Private poroperties
    privateName = "Ludwig the " + initial + "th";

    return that;
};

// Create objects
var newPar1 = parent(42);
var newPar2 = parent(10);   

var newChild1 = child(0);
var newChild2 = child(100);

// Output on the jsFiddle page refed above: http://jsfiddle.net/ghMA6/
Peter Ajtai