Javascript Object-Oriented Syntax Reference [closed]
-
16-04-2021 - |
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:
Books
- JavaScript: The Good Parts by Douglas Crockfond. This is the book recommended by most of Javascripters on Stack overflow. I observed, People have advanced their skills in java script with this book. Visit his website: crockford.com for more information.
- Object-Oriented JavaScript: Create scalable, reusable high-quality JavaScript applications and libraries by Stoyan Stefanov, goes through many OOP topics such as Objects, Prototype, Inheritance and some patterns.
- Pro JavaScript Design Patterns, by Ross Harmess and Dustin Diaz, discuss application of famous design patterns in JavaScript.
- JavaScript Bible: This covers the new powerful functionality JavaScript
Videos
- "Advanced JavaScript" videos by Douglas Crockford. Many other interesting videos are available at Yahoo! Developer Network.
On Stack Overflow
- A Stack Overflow discussion on JavaScript closure
- A Stack Overflow discussion on What exactly does “closure” refer to in JavaScript?
- A Stack Overflow discussion on Prototypal vs Functional OOP in JavaScript
Others
- Introduction to Object-Oriented JavaScript - Modzilla
- JavaScript Closures for Dummies
- JavaScript Closures
- JavaScript Closures 101
- Private Members in JavaScript by Douglas Crockfond
- Classical Inheritance in JavaScript by by Douglas Crockfond
- Prototypal Inheritance in JavaScript by by Douglas Crockfond
- JavaScript, time to grok closures
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
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.