Discover the best of the web!
Learn more about Digg by taking the tour.
Object-Oriented Javascript
nefariousdesigns.co.uk — "Although Javascript shouldn ’t be classed as an object-oriented language, pretty much everything within it is object based; from DOM scripting (Document Object Model) through to specific built-in objects such as Image and Date."
- 685 diggs
- digg it
- sandrat44, on 10/12/2007, -9/+7Reads like a textbook on JS. Sorry, already have an O'Reilly book on it. No commentary on OO JS so it was little use in my case, sorry no digg. Will others find it digg worthy?
- MeridianBlade, on 10/12/2007, -14/+2DUDE JS is the way to go! regardless if it takes 2 pages of code to achieve something that takes three lines in C or Pascal!
- sandrat44, on 10/12/2007, -13/+3Well... guess I was wrong on how many diggs it would get! Are there really that many out there that don't already own a JS textbook, or is this just for showing support for JS - like MeridianBlade?
Oh, heck... I see Kevin Rose dugg this too, sorry dude... I'll go digg it right now! - btipling, on 10/12/2007, -0/+12I can't believe you digged something because Kevin Rose did. Do you have no shame?
- sandrat44, on 10/12/2007, -4/+1Oh do behave, baby - Just for sh*ts and giggles.... yeah baby, yeah ;)
but I did accidentally digg it - as I was reading another article and thought I was digging it, ooops. Sort of like when Austin Powers drinks that "nutty" coffee...
In all though, if some newcomers to JS are learning something from it, perhaps they'll find some of the more interesting comments with links to better material that I just +digged when they were down. Now why these poor lads, making good points were -digged I don't get. - stardustwd, on 10/12/2007, -0/+2MeridianBlade: How can you compare JavaScript to C or Pascal? You obviously don't know what you're talking about. JavaScript is a web based scripting language whilst the others are application based programming languages. Sure you probably can do something quicker in C or Pascal, but not on the web as they are programming languages.
- revoked, on 10/12/2007, -5/+2wow, this is sort of three years ago. instead lets talk about absctraction instead:
http://ajaxian.com/archives/google-web-toolkit-the-correct-level-of-abstraction- Bret, on 10/12/2007, -4/+5that's for wussies
This is one of the better writeups on OO js i've seen
- Bret, on 10/12/2007, -4/+5that's for wussies
- CharlesDarwin, on 10/12/2007, -8/+3The tutorial isn't that great, and the site's design is ***** awful. Why have the text run across just half of the screen?
- rasterbator, on 10/12/2007, -0/+2So you can load ghosted back Google ads of course.
- Fett101, on 10/12/2007, -0/+5Text runs across only half because people tend to prefer that. If the lines go on too long they tend to get lost at times, lose what place they were reading and skip to other colums. Or they may get bored and annoyed with how long the text seems to be.
- brilliant, on 10/12/2007, -2/+0What the.................
That site's design is good and could easily qualify for CSSzengarden.
You just need to quit trollin' and hatin' .
>and the site's design is ***** awful.
- skidooer, on 10/12/2007, -1/+7"Although Javascript shouldn’t be classed as an object-oriented language"
But it is an object-oriented language; of the prototype genus.- Bret, on 10/12/2007, -1/+3right. I've heard people profess that OO is impossible in js. Not True--there are simply no language constructs that abstract the unsightly, arguably unintuitive prototype syntax.
- merreborn, on 10/12/2007, -1/+4According to wikipedia, JS is "object-based", which basically means "object-oriented, but missing some key features like polymorphism and inheritance".
So, if you want to be pedantic, there you have it.
http://en.wikipedia.org/wiki/Object-based_programming - skidooer, on 10/12/2007, -0/+6But, also according to Wikipedia: "Prototype-based programming is a style of object-oriented programming"
http://en.wikipedia.org/wiki/Prototype-based_programming - heinousjay, on 10/12/2007, -0/+8Javascript has no need of inheritance or polymorphism - it's not statically typed.
Javascript is a very interesting language. It's completely object oriented (even primitives and functions are manipulable objects), it supports functional programming, and it has default objects in its scope chain that allow a procedural style of programming if desired. Most programmers just don't understand the power the language unleashes. I think they get fooled by the C style syntax. - at2000, on 10/12/2007, -2/+0http://en.wikipedia.org/wiki/Object-based_programming clearly says: JavaScript is "object-based" because it is in the second category (prototype-based), but not that it missed some programming construct. Obviously JavaScript has inheritance and polymorphism.
- SanityInAnarchy, on 10/12/2007, -0/+3JS is also flexible enough that -- like Ruby -- it shouldn't be too hard to implement whatever features are lacking.
Well, that's a guess. Could you provide some examples of syntax you don't like, and what you'd replace it with? - yahoofrom, on 10/12/2007, -0/+1@heinousjay
Would you enlighten us how javascript supports functional language? you mean, like haskell and lisp? - krouskop, on 10/12/2007, -0/+0In reply to yahoofrom's question, "Would you enlighten us how javascript supports functional language? you mean, like haskell and lisp?"
From Wikipedia [http://en.wikipedia.org/wiki/Javascript]:
"JavaScript is also considered a functional programming language like Scheme and OCaml because it has closures and supports higher-order functions. The Little Javascripter shows the relationship with Scheme in more detail."
- Bogtha, on 10/12/2007, -0/+7> Javascript shouldn’t be classed as an object-oriented language
Of course it should. The only reason some people say otherwise is because they are familiar with class-based object-oriented languages, and JavaScript is a prototype-based object-oriented language. Both styles are object-oriented; supporting objects, methods, properties, polymorphism, etc.- sandrat44, on 10/12/2007, -4/+1ditto oh master of disaster, the ayatollah of rock and rolla, the script keeper ;) +digg for you.
- sandrat44, on 10/12/2007, -4/+1ditto oh master of disaster, the ayatollah of rock and rolla, the script keeper ;) +digg for you.
- mck9235, on 10/12/2007, -2/+4Oh, quit your moaning people, I found it useful, it explained more about OOP and JS for me. I finally know what the prototype thing is, here I thought it actually had to do with Prototype. . . Definitly digg worthy.
- Fizban119, on 10/12/2007, -5/+0I like Object-Oriented-Programming. It's more intuitive than regular programming. My favorite OOP program - Alice.
- dupy, on 10/12/2007, -2/+3Interested in serializing your JS objects? Well, check out JSON (JavaScript Object Notation) - http://www.json.org. When I first saw the title of this digg, I thought "oh, they must be speaking of JSON."
And yes, I believe JS should be classified as an OO language too. I am not too picky about semantics, and to me it fits my description of OO just nicely. ;) - tehmoth, on 10/12/2007, -0/+5douglas crockford's site http://javascript.crockford.com/ does a better job of explaining object oriented javascript and the problems with the new ECMA draft.
- Jerf, on 10/12/2007, -0/+4This article is bad; not complete garbage, but bad. Several of the example code bits are completely non-functional. For instance, in the "new Operator" section, the last code snippet in that section (right above "Encapsulation") should read "getArea(obj_pizza)", not "obj_pizza.getArea()", as nowhere on the page before that is the getArea method added to the Circle prototype. Also in that section there are inconsistencies in the capitalization of Circle.
These are merely samples of one section; there are many other errors in every other section. This has the net effect of only being useful if you already know enough Javascript to correct the errors in the piece.- covertbadger, on 10/12/2007, -1/+1@Jerf - have you actually tried running the code you claim is non-functional? If you understood js as well as you seem to think you do, you would realise that the line 'this.getArea = getArea;' in the circle definition makes the getArea function available, thus 'obj_pizza.getArea()' is perfectly legal and works as expected. I suggest you brush up on your programming skills before spouting off in future.
- Jerf, on 10/12/2007, -0/+0Covertbadger: That was corrected since my comment was made. I note the capitalization was also corrected. The content is much better now and basically digg-worthy.
- WilliamTanksley, on 10/12/2007, -0/+3JavaScript is (as others have said) definitely object-oriented. As still others have said, it's prototype based rather than class based. I'm posting to point out that prototype-based OO is a strict superset of class-based OO; superior in most ways (don't believe anyone who claims TOTAL superiority for anything ;-). http://en.wikipedia.org/wiki/Prototype-based_programming gives a fair presentation (although it fails to note that prototype systems are not classless -- you can /easily/ implement classes using a prototype system). Google for "delegation prototype" for more info, or try http://javalab.cs.uni-bonn.de/research/darwin/project.html if you like either static typing, Java, or well-written research.
- bpapa, on 10/12/2007, -0/+1I'm by no means a Javascript guru, but the recent "AJAX in Action" book really opened my eyes to a lot of things that I didn't know about js. I think it's a worth a look whether you are interested in building AJAX apps or just want some extra insight into js.
- armbar, on 10/12/2007, -0/+1I'm not trying to comment-spam here, but there's a lot of info about "new" things you can do with JavaScript in articles across the web--I don't think you really need to go buy a book unless you want something to read on the crapper. We have a pretty good collection of articles in my Magnolia group: http://ma.gnolia.com/groups/AdvancedJavascript
- armbar, on 10/12/2007, -0/+1I'm not trying to comment-spam here, but there's a lot of info about "new" things you can do with JavaScript in articles across the web--I don't think you really need to go buy a book unless you want something to read on the crapper. We have a pretty good collection of articles in my Magnolia group: http://ma.gnolia.com/groups/AdvancedJavascript
- NSMike, on 10/12/2007, -8/+0Dude. My object oriented class in college USED Java to teach object oriented programming.
- Phoenyx, on 10/12/2007, -0/+5Javascript and Java are not the same--not even related except for some similarities in syntax.
- gordigor, on 10/12/2007, -3/+1I don't know if your trying to be funny or not but...uh, you do know that Java and Javascript are two totally separate languages.
- NSMike, on 10/12/2007, -1/+0Actually, no, I didn't know that. Since that semester I ended up with a D in my object oriented class and I failed my Data Structures class, I ended up dropping my Computer Science major and went to something else. I don't really keep up with development stuff anymore.
- sandrat44, on 10/12/2007, -2/+1@ NSMike: No wonder you're getting burried...
But, why are some bozo's burrying legit comments? Please -digg my goofy comments that were just for fun, but when someone is commenting and trying to make a valid point, even if it differs from yours, it sure makes it hard to go look in those burried comments to follow the argument.
Yeah, not eveyone will agree on something, but as long as it's a discussion on the subject promote those, and burry our goofy, silly, off subject comments to make the comment section more readable!
- dk911, on 10/12/2007, -0/+8The problem isn't that people misunderstand Javascript. It's that people misuse Javascript or have never used it for anything more than a User Interface language. If people actually put to practice what they know about Javascript, they'd see a complex language with more than one way to skin a cat. You see, people typically associate Javascript with other "scripting languages" and avoid the fact that it is fully object oriented and can handle tasks that other languages require jumping through hoops to achieve.
This author is showing only a fraction of what Javascript is capable of. It being a fully operational language that allows object oriented design and development. Consider that many of the web 2.0 technologies have chosen it as their foundation on the client-side. Instead of using technology such as Flash or Java Applets, these applications have chosen to use DHTML and Javascript as their core. What they are missing, however, is an application that really utilizes the language.
Most applications are built upon Prototype or some similar framework. These applications use very little of the language. They, instead, are built upon an existing framework and do very little to venture outside what is known of the language. People hailed Google Mail as the web 2.0 application to end all applications. It was the "piece de resistance" that people pointed to when referring to Javascript's future. What it doesn't do is really exercise the language.
The language has many different ways to approach the same situation. That is why I find myself enthralled by it. You can do the same operation in so many more ways than you can in languages which are used in more traditional programming. Then again, this is what makes Javascript so hard to use. Since you can approach the development for a simple object from 5 (that I know of) paradigms, it can be confusing to some to develop in this language.
Many consider Javascript to be nothing more than a simple scripted language. Yet, it runs many of the applications that we are all familiar with. It is at the core of AJAX and JSON, and makes web applications more functional. Yeah, Flash and Java Applets have their place, but they are just icing on a cake that already tastes fairly good. What is missing from the landscape is a truely enterprise level application written in Javascript.
Now, you might think that it's impossible, and for the most part it is. Entirely written in Javascript would require that the language be able to read and write to the client system and do other system level tasks. Javascript itself cannot do these things, but with the proper back-end, Javascript can do anything. It truely *is* an amazing UI language, but at the same time provides the structure in an unstructured world. I know that may sound a little pragmatic, but it's true when considered amongst other languages. How many other languages can claim to be the glue that holds the web experience together?
Ok, so I'm getting a little airy here. My point was to show that Javascript has a flair for being a language that holds tough when other languages just cannot compare. Having spent the last 8 months of my life developing a framework that operates in Javascript (and converses with Java vis JSON). And then on top of that developing an Enterprise Application that runs in any browser that supports Javascript (ECMAScript) to its fullest, I feel that I have a certain level of expertise in the subject.
I have spent so much time with the language, examining its nuances and it's shortcomings that it has become a language that I am very attached to. You might call me a "fanboy" but I really am just a proponent of a language that can do things like no other. So, for someone to say that this language isn't truely Object Oriented is like someone saying that the sky isn't really blue. It's a matter of perceptions, but Javascript is a powerful language that can produce amazing results and does provide Object Oriented functionality. Unlike some languages that claim to have it all and then prove to be too cryptic to use.
Ok, I need to step down... it's getting late.
- dk_911- sandrat44, on 10/12/2007, -0/+1Well said, long.... but well said.
It's been a while since I've had to code in JS, I got promted and have to see over the work of others and manage projects... boring as hell...
speaking of nuances, the one thing that really torqued me off was the NN/IE *****-fight to dominate the browser scripting language and having to browser check due to slight differences in DOM... I'll check with some of the other developers around here, but has this issue mellowed out yet? I still see some browser checking code floating out there... - armbar, on 10/12/2007, -0/+1No, the issue hasn't really mellowed out yet, but there are things you can do to avoid browser detection, which is evil.
The majority of DOM based scripting is the same--most of the time you'll be using getElementById or getElementsByTagName, which work in every browser on the planet (basically). The major differences for application development, in my opinion, are in event handling and Ajax calls, but of course those can be wrapped in another object. Prototype does this automatically for you if you're feeling lazy.
Just don't revert to the bad old days and start using IE's DHTML scripting (a la document.all, etc.). Those are the proprietary functions and properties that aren't widely supported.
- sandrat44, on 10/12/2007, -0/+1Well said, long.... but well said.
- pornel, on 10/12/2007, -0/+3"Although Javascript shouldn’t be classed as an object-oriented language"
*****. Reported as inaccurate. JS is Object-oriented, it just doesn't have classes and strict typing, but everything in JS is an object, even functions are objects.
It has prototype-based inheritance (which you can extend/modify), you can have encapsulation using constructors' scope and accessor methods.
http://www.crockford.com/javascript/javascript.html (see end of the article). - krouskop, on 10/12/2007, -0/+0"Although Javascript shouldn’t be classed as an object-oriented language" ... umm, no: JavaScript is a prototypical object oriented language. More info: http://www.cs.rit.edu/~atk/JavaScript/manuals/jsobj/index.htm
- harry4000, on 10/12/2007, -0/+0There are at least two super common misconceptions about JavaScript:
Eye opener #1: JavaScript is OOP. JavaScript, ActionScript, ASP/JScript, and Windows Scripting Host have the same fundamental power as every other OOP language. ECMA just needs to market these 4 languages better. If you don't know who ECMA is and what they have to do with all this, then that's my point. They're too humble about the standards they've created.
Eye Opener #2: Why do people waste their time and clutter their code by terminating each JavaScript line with a semicolon? Semicolons are functionally useless and your code would work just fine if you destroyed every one of them. I'll let you know if I ever decide that my code needs Christmas decorations, but until then no semicolons for me.- UselessPickles, on 10/12/2007, -0/+0One good reason to terminate every line with a semi-colon is that the code will still work properly if you strip out all comments and white-space to reduce the code size. I think the decision to make semi-colons optional at the ends of lines was a bad decision - probably because the language was initially designed to be used by non-programmers that would be more likely to forget semi-colons.
Digg is coming to a city (and computer) near you! Check out all the details on our