Question

I am new in Javascript but I did a lot of C#, VB.NET and Java programming that those languages are fully object-oriented. It seems Javascript cannot support all OO features.

I am looking for a Javascript object-oriented syntax reference.

What should it include is

  • Inheritance syntax
    • Call parent constructor in child constructor
    • Refer to the instance of parent object
    • Multiple inheritance (It seems it is OK for javascript)
    • Inheritance type checking
  • Static/Shared methods
  • Public fields
  • Protected/private fields (I am not sure Javascript have it)
  • Interfaces?

No correct solution

OTHER TIPS

It's not that Javascript doesn't support all OO features. Every concept from classical OO feature can be implemented in Javascript, the questions is if it's really necessary to transpose all these concepts to it. Keep in mind Javascript is a prototype language, most of the times it's much easier to use this for your own good instead of try to achieve all features from other static type languages (though it's not considered evil).

About the topics you mentioned:

  • Call parent constructor in child constructor:

    Since Javascript is a prototype language there is no parent classes, just parent objects. If somehow you need to call a parent constructor from a descendant object your design might be wrong. Parent objects might already exist when you produce a derivate (descendant) object from it, so there is no need to call a constructor again, it sounds pretty strange even in theory.

  • Refer to the instance of parent object:

    AFAIK it's not possible unless you create a reference property on your child object.

  • Static/Shared methods:

    There is no formal implementation for this in Javascript. Every method can be accessed like a static method. See example:

    Person.sayHello = function(){
        alert("Hello!");
    }
    
    Person.sayHello();
    new Person.sayHello();
    
  • Protected/private fields There is a shiny addition to ECMAScript 5.1 (see my second link for legacy private member support.). Now you have defineProperty method, the code explains itself:

    var cat = {};
    
    Object.defineProperty(cat, "name", {
      value: "Maru",
      writable: false,
      enumerable: true,
      configurable: false
    });
    
    Object.defineProperty(cat, "skill", {
      value: "exploring boxes",
      writable: true,
      enumerable: true,
      configurable: true
    });
    

And here some links that helped me a lot and some that I've read recently:

  1. About Classical inheritance in Javascript
  2. Private members in Javascript
  3. Javascript Additions in ECMAScript 5.1

Books

Videos

On Stack Overflow

Others

Read Javascript, the Good Parts by Douglas Crockford, and you will get what you are looking for.

Javascript is a prototypal scripting language. These links should get you started.

Wiki - Prototype base programming

Understanding javascript prototypes

OOP in Javascript

Get started with John Resig's Pro JavaScript Techniques and "Javascript, the Good Parts" by Douglas Crockford.

Do not write javascript like Java. They aren't the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top