Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Object Oriented Concepts in Javascript

I have been using Java for a long time, and for quite some time I was doing web development using GWT (Google Web Toolkit). The beauty of it was that I had my Java object oriented constructs and would not worry about how it gets translated to GWT - let Google take care of it. My knowledge of Javascript was sufficient but not to the extent that I could do heavy web development with it. Later I decided that I have to get a deeper and more thorough understanding of Javascript and this has been a real roller coaster - just at a time where I think I get something, something comes and proves me that I was wrong - that I simply misunderstood.

What better place to voice my concern than stackoverflow: I am begging for some resources and pointers to what would be a Javascript equivalent to some of the following Java concepts:

Class
instance of a class - object
Member variables
Getters
Setters
Abstract Class
Interface
Inheritance
Access Modifiers
Constructors

I know some of those concepts, but as I said - I believe I have some conceptual difficulties. If someone could point to a real javascript guru's attempts to pinpoint these very concepts here I would be very happy.

like image 802
oneiros Avatar asked Feb 15 '12 15:02

oneiros


3 Answers

I think you mainly need to understand that Javascript is a pure OOP language, but it doesn't have classes!... That to a Java programmer is quite a shock, and takes a while to get your head around it, but is a quite powerful paradigm.

This video here is from Douglas Crockford, a guy who helped creating Javascript, brilliant for new Javascript programmers

http://www.youtube.com/watch?v=v2ifWcnQs6M

like image 130
Alberto Gutierrez Avatar answered Sep 25 '22 05:09

Alberto Gutierrez


Douglas Crockford explains how to mimic these Object Oriented features very well. His book JavaScript: The Good Parts is one that I think everyone should read and explains how to get the most out of JavaScript's often confusing features.

Try this brief tutorial of his for a basic way to get private class methods and properties via closures. Also, this tutorial will show you how to achieve classical inheritance.

like image 26
Matt Gibson Avatar answered Sep 25 '22 05:09

Matt Gibson


I've been at the same quest as you but I've had to scrape and fetch knowledge all over the place.

There are tons of excellent posts all over stackoverflow for all these subjects, and then there is MDN I would also recommend peering into the source of popular libraries like jquery. See this source viewer http://james.padolsey.com/jquery/

This is a BRILLIANT interactive tutorial by the great John Resig:
http://ejohn.org/apps/learn/

Here are some very good SO posts that helped me understand JS better:

How to "properly" create a custom object in JavaScript?
What is the 'new' keyword in JavaScript?
Why is JavaScript prototyping? Why is it necessary to set the prototype constructor?
Call base method in Javascript using Douglas Crockford's functional inheritance
Help understanding jQuery's jQuery.fn.init Why is init in fn
What does jQuery.fn mean?
Why 'this' resolution is so special in JavaScript?
What is the difference between call and apply?
Dynamic function call (apply)
JavaScript data formatting/pretty printer
Checking if a key exists in a JavaScript object?

Here are some posts about the quirkyness of javascript and stuff you prolly didn't know:

Is it possible to reflect the arguments of a Javascript function?
function arguments
What is the !! (not not) operator in JavaScript?
How does this JavaScript/JQuery Syntax work: (function( window, undefined ) { })(window)?
Which equals operator (== vs ===) should be used in JavaScript comparisons? Behavior of delete operator in javascript
var myArray =[], name;?
Why is null an object and what's the difference between null and undefined?
Checking for null/undefined in JavaScript
What does the exclamation mark do before the function?

like image 26
gideon Avatar answered Sep 26 '22 05:09

gideon