admin管理员组

文章数量:1194948

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.

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.

Share Improve this question asked Feb 15, 2012 at 15:23 oneirosoneiros 3,56812 gold badges45 silver badges73 bronze badges 4
  • 1 Its a good question but I am voting to close as its scope is very broad. – Perception Commented Feb 15, 2012 at 15:26
  • 3 The thing is, half of those concepts don't exist in JS, partly due to prototypal OOP and partly due to dynamic typing. You won't get very far trying to translate individual concepts. – user395760 Commented Feb 15, 2012 at 15:28
  • 2 I vote against closing because he does not want us to explain this stuff. He only wants links to good resources. – J. K. Commented Feb 15, 2012 at 15:28
  • The first two chapters of a course I'm working on explain some of these concepts, particularly scope, inheritance and constructors. eventedmind.com/courses/1-advanced-javascript-concepts. – cmather Commented Jun 19, 2012 at 7:35
Add a comment  | 

8 Answers 8

Reset to default 6

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

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.

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?

Well, the basic rule of thumb in JavaScript is that it will let you do nearly anything. There are no access modifiers. Since JavaScript doesn't prevent very much, things like abstract class just don't make sense. You can always instantiate it.

Vaguely class like behavior comes from Objects and you can get constructor like behavior using prototype. http://www.javascriptkit.com/javatutors/proto.shtml

Member variables are similar but untyped, and can also contain functions as well as data.

In JavaScript you need personal discipline, and strong code conventions to simulate any of those things, but the language will not help you a bit. It's ultra flexable, which means it doesn't get in your way, but it also gives you plenty of rope to hang yourself too.

It's best to approach it as a completely different language. Don't try to relate it to Java, because it's fundamentally different in many ways.

The name thing is the most related part of it all which creates much confusion...

JavaScript is pure OO language, however it lacks some of the concepts of the class based OOP languages like Java or C# However the good news is that the good folks of ECMA are working on the matter to bring OO concepts like abstraction, classes, namespace, inheritance, property... into JavaScript

Peter Michaux has a good explanation on this

I can think of a couple references that will answer most of these questions, including explicitly stating that JavaScript is a loosely-typed, dynamic language versus Java as a strongly-typed, static language. I feel this is the type of direct analogy you are seeking.

Many general questions from the first five items listed will have light strewn upon them with a quick read of the "Working with Objects" section of the JavaScript Guide at the Mozilla Developer Network. (Class, instance of a class - object, Member variables, Getters, Setters)

The second group of items I think are explained very well in this site from Douglas Crockford, who wrote the O'Reilly book "Javascript: The Good parts" as well as the JSLint code tool. (Abstract Class, Interface, Inheritance, Access Modifiers, Constructors) Crockford makes very direct analogies between Java and JavaScript in a page that describes how JavaScript supports not only classical inheritance, but other code reuse patterns as well.

For some information on JavaScript variable scoping have a look at this article. It explains the basics of creating public, protected and private variables for object prototypes. http://www.anzaan.com/2009/05/javascript-variable-scope-private-protected-and-public/

I have had a very similar shock to the one that you have had when trying to go into Javascript. Javascript the good parts by Douglas Crockford and his site http://crockford.com/ have proven to be of immense help for me.

本文标签: Java Object Oriented Concepts in JavascriptStack Overflow