Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In a meeting with one of the architects in digital firm, I was asked what the difference is between object oriented and non-object oriented javascript.

Unfortunately, I couldn't answer this properly and I just answered I thought javascript was object oriented language only.

I know with object oriented design for javascript you can have usual oop procedures such as polymorphism.

What do we think of this? Is there such difference and can we separate the two?

share|improve this question

closed as not constructive by FelipeAls, Aleksandr M, Tom Redfern, bipen, DBD May 1 '13 at 13:18

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers 2

up vote 2 down vote accepted

Most object-oriented languages can be used in a non-OO way. (Most non-OO languages can be used in an OO way as well, come to that, you just have to make an effort.) JavaScript is particularly suited to being used in both procedural and functional ways (and is also very well-suited to being used in various OO ways). It's a very, very flexible language.

For instance, here are two ways to write something that needs to deal with information about people, saying how old they are:

Procedural:

// Setup
function showAge(person) {
    var now = new Date();
    var years = now.getFullYear() - person.born.getFullYear();
    if (person.born.getMonth() < now.getMonth()) {
        --years;
    }
    // (the calculation is not robust, it would also need to check the
    // day if the months matched -- but that's not the point of the example)
    console.log(person.name + " is " + years);
}

// Usage
var people = [
    {name: "Joe",      born: new Date(1974, 2, 3)},
    {name: "Mary",     born: new Date(1966, 5, 14)},
    {name: "Mohammed", born: new Date(1982, 11, 3)}
];
showAge(people[1]); // "Mary is 46"

This is not especially object-oriented. The showAge function acts on objects given to it, assuming it knows the names of their properties. It's a bit like a C program working on structs.

Here's the same thing in an OO form:

// Setup
function Person(name, born) {
    this.name = name;
    this.born = new Date(born.getTime());
}
Person.prototype.showAge = function() {
    var now = new Date();
    var years = now.getFullYear() - this.born.getFullYear();
    if (this.born.getMonth() > now.getMonth()) {
        --years;
    }
    // (the calculation is not robust, it would also need to check the
    // day if the months matched -- but that's not the point of the example)
    console.log(person.name + " is " + years);
};

// Usage
var people = [
    new Person("Joe",      new Date(1974, 2, 3)),
    new Person("Mary",     new Date(1966, 5, 14)),
    new Person("Mohammed", new Date(1982, 11, 3))
];
people[1].showAge(); // "Mary is 46"

This is more object-oriented. Both the data and behavior are defined by the Person constructor function. If you wanted, you could even encapsulate (say) the born value such that it couldn't be accessed from other code. (JavaScript is "okay" at encapsulation at the moment [using doing it with closures]; it will be getting a lot better at encapsulation [in two related ways] in the next version.)

share|improve this answer

It's entirely possible to code a working piece of Javascript without ever declaring a class or a prototype. Certainly you'd use objects, since the API and the DOM are made up of them. But you wouldn't really need to think in an OO manner.

But it's also possible to code Javascript in a thoroughly OO way - create lots of classes/prototypes, take advantage of polymorphism and inheritance, design behaviour in terms of messages passed between objects, and so on.

I suspect that this is the distinction the interviewer was hoping to get from you.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.