Discussion on:

7
Comments

Join the conversation!

Follow via:
RSS
Email Alert
0 Votes
+ -
It is surprising how many JavaScript "experts" don't know about the OO features of the language. I'd like to add a few things learned over the years.

1. Inheritance is much easier if you remove everything but class structure definitions from the constructor. I always use an initializer method that's called from the constructor but is a separate method.

2. It is true there is no method overloading. This is because JavaScript is a weakly typed language (let's be controversial here and say -- like all good OO languages should be!). The concept of method overloading is only relevant in a strongly typed language.

3. You forgot to mention that JavaScript does not automatically let you call overridden parent methods. Because you override by assigning a function pointer to a method name you are then unable to access the original (the parent's) method. What I always do for overriden methods is define a new method called parent .prototype = new (); syntax, you MUST make sure that the parent class constructor willrun successfully with NO PARAMETERS.

There is other syntax that allows inheritance but I've found them to be somehat flaky and to not always work. I have never found the syntax you showed to fail but you need to make sure the constructor has no required parameters (separating the initialization helps).

5. I've found onject factories to be of great use with JavaScript (as they are with other OO languages).
Thanks for the 5 points.
pramod
Just like to say thanks for this artical.
It will help. happy
The article was very helpful, but wanting in some of the more familiar notations for those from other languages...

A couple of other tricks to help:
The article describes the format
function ClassName ()
{
this.action = action ; function action () { ... }
}

while this form will seem sort of familiar and simialar to the C++ notation, ie, void action() ;
and then a method declaration elsewhere, it's sort of lost on Java developers or Python users who expect to seea method. What's worse, it's disjointed...

so...

function ClassName () {
this.action = function() {
// this inlines a method
// ...
} ;
}
// the ";" needs to be there,
// technically, just like for
// a class in C++...

Also, JavaScript is very flexible with its objects, just like Python or Java, so it does allow anonymous object creation (which to some sort of defeats the purpose of objects--reusable components -- but it also helps with thingslike the factory pattern where you need a factory...)

This has an equal in an anonymous inner class in Java or a lamda function in Python...

var factory = new ( function()
{ /* object def here.. */
this.create = function() {} ; //etc..
} ) () ;
The above line of code starts off normaly,
var factory = new .. and then, a definition is slipped in, between parenthesis, to force evaluation, and then the () is reached which invokes a new object...
0 Votes
+ -
Nice
SoftwareGuru 7th May 2003
I knew about this feature but had never seen it explained clearly. Thanks
0 Votes
+ -
Javascript is nor "rudimentary" as the author implies from the fact that it doesn't support classes.
You forgot to mention Javascript is a Prototype-based language, as opposed to the majority of the languages du jour, that are indeed Class-oriented.
There are other Prototype-based languages such as Self, which AREN'T incomplete or rudimentary, they just chose a different architecture design.
It's the same when people think Java is rudimentary because it has Interfaces instead of multiple inheritance.

To have Object Oriented Programming the only thing you need is objects.
Classes, Interfaces, Constructors, Destructors, Mix-ins, etc. are just TOOLS that are used to build Objects, and that may vary from language to language.

If you cared to explain why you think this is rudimentary, at least you could have been fair telling your audience Prototype-based languages is a preference of the language designer, not an omission.

Oh Well !!

vruz ( at ) internet ( dot ) com ( dot ) uy
0 Votes
+ -
.so nice
fuhtahxhit 21st Aug
This article can really help the teachers who teach about programming and for the student that taking up Bachelor of Science in information technology.
Keyboard Shortcuts:
Prev
Next
Toggle
Join the conversation
Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]

Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion.