0

I have done alot of OOP for long now (with C++/Java/Python etc) but world of JS is confusing. I wanted to try it by writting Ajax library (I know there are libs but this for JS OOP familiarization) and found confusing. No class keyword et cetera. What is happening? Am I missing a piece of puzzle here? what differentiate functions from Classes? Where is private/public?

Please help me clear issues here!

4
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Troy III
0

Douglas Crockford has a really straightforward discussion on classical inheritance with Javascript here http://www.crockford.com/javascript/inheritance.html. He also presents a decent lecture on yui theater. Basically while everything can be viewed as objects, OOP in javascript isn't the same at all as in other languages since it is classfree and loosely-typed. Classes are functions in this language (which are also objects). Methods of a class are really just properties of a given object.

An example "class" (please note that "method" is not apart of JS and is a helper function written for the given example) from the the above mentioned site is:

function Parenizor(value) {
    this.setValue(value);
}
/****
 * Note this could be written as Parenizor.prototype.setValue = function(){}
 */

Parenizor.method('setValue', function (value) {
    this.value = value;
    return this;
});

Parenizor.method('getValue', function () {
    return this.value;
});


Parenizor.method('toString', function () {
    return '(' + this.getValue() + ')';
});
Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};

You still create new instances of the object Parenizor by "new". Also you still access "mehtods" of the class using the dot notation.

Another little tutorial is here: http://www.javascriptkit.com/javatutors/proto.shtml
NOTE: The prototype here is not the same as the Prototype framework for ajax and dom manipulation.

Edited by scrappedcola: n/a

0

Thanks for the Post.
I will read the tutorial. I still don't understand the difference.
Let me read the links. Thanks for replying and sorry for late response

0

Good one. :) I elaborated bit more at JavaScript Confusing Bits.

It is important to understand it is realy broad topic and there is always more than way of solving a problem. In terms of inheritance with modular pattern and priviledged memebers almost any type of inheritance could be replicated. However it is worth of considering if using classical inheritance for example is way to go. JavaScript is prototyping language with good model of prototype chain which expanded is more than enough in most cases.

Also if you going for OOP in JS perhaps you should look at how it has been done in prototype.js or coffescript.js. Those guys seem to perfect the concept.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.