Jump to content

Object-Oriented Programming in JavaScript, anyone?


webdesigner25

Recommended Posts

What do you mean? What objects you create depend on the purpose of your code.P.S. JavaScript is a prototyped language, and thus does not have classes. Also, technically it is not possible to write non-OO code in JS, but it is possible to write in a very procedural fashion (e.g. like writing everything in the main method in Java or C++).

Link to comment
Share on other sites

I wrote a tutorial on basic OOP in JavaScript http://www.dev102.com/2008/10/14/object-or...ith-javascript/.Mozilla also has a great introduction https://developer.mozilla.org/en/Introducti...nted_JavaScript
nice links, thanks! :)
Link to comment
Share on other sites

What do you mean? What objects you create depend on the purpose of your code.P.S. JavaScript is a prototyped language, and thus does not have classes. Also, technically it is not possible to write non-OO code in JS, but it is possible to write in a very procedural fashion (e.g. like writing everything in the main method in Java or C++).
to answer your question, i am talking about reusable code that you use across projectsthough, telling people javascript doesn't have classes isn't very helpful these days, but what do u mean by "not possible to write non-OO code?"
Link to comment
Share on other sites

Even an "ordinary" function is technically a method of the window object. You don't have to explicitly write it that way; it just is.And the difference between a class and a prototype is only useful if you're discussing computer science as a science. They're the same kind of thing, generally, so for everyday programming, it is perfectly safe to think about them the same way. Synook mentioned all that in a PS (I suspect) to suggest that it wasn't so much an answer to your question, but more of a "good to know" kind of thing.The tutorials linked above show you how to create object constructors. (All code is more or less reusable. Most of us have little libraries of code we reuse. Some it may involve objects, some of it won't.)

Link to comment
Share on other sites

Oh! I personally learned to program before OOP existed, so I mostly stick to procedural habits. I have a few object libraries for AJAX and cookies. What I like to do a lot of, however, is reprototype JavaScript's built-in objects so they have more methods. I do that especially with the string and array prototypes, because I'm accustomed to having a wider variety of functions available when I work in PHP, and I like to have similar stuff in JavaScript. That's me.

Link to comment
Share on other sites

Oh! I personally learned to program before OOP existed, so I mostly stick to procedural habits.
You learned programming before the 1960's?OOP is wonderful in my opinion. When you first look into it you say to yourself, "Ok, I could have done this before without OOP and its complicated syntax. No thanks!". But now I use objects for everything I can. It makes updating easier and programming simpler. The idea of OOP is that you can create an infinite amount of objects (up too your max memory anyway) with their own personal variables. Lets say you have a "People" program. You want to record the people in your life for whatever reason.Family:Steven - FatherJane - MotherJames - BrotherJordon - BrotherMaggie - SisterFriends:KirkJimmyHaleyNow WITHOUT OOP you might have to do something like this-
janeHair = "black";janeEyeColor = "brown";function getJaneHairColor(){   return janeHair;}

That's going to get tedious real quick! Adding new members will be a waste of time. In OOP you would instead be able to do something like this-

Jane = new Person();Jane.setEyeColor("brown");Steven = new Person();Steven.setHairColor("Blonde");alert("Jane has " + Jane.getEyeColor() + " colored eyes while Steven has " + Steven.getHairColor() + " colored hair"):

Organization becomes very easy. It might be overkill for some JS scripts but I use objects in PHP for member login.BUT TO ANSWER YOUR ORIGINAL QUESTIONI've used objects for a js battle system. A game where it got information about a character from a database with php and turned it into a js object where I could manipulate it in a battle scene.

Link to comment
Share on other sites

You learned programming before the 1960's?
OOP wasn't in a lot of programming languages or even popular until the 90s.The idea may have been around for ages but its believing in that idea. Lisp is an awesome language, one of the fore farther of today. It's 50 years old and still no other language has come close to it.
Link to comment
Share on other sites

I believe, Mr. Fish, I was thinking of the widespread implementation of languages like C++. Clearly that was an exaggeration. :) Even Steve Jobs took a pass on OOP when he toured Xerox PARC. He grabbed the basics of the Alto's GUI but missed out on SmallTalk (a blunder he later regretted). This is the era to which I was referring.

Link to comment
Share on other sites

You learned programming before the 1960's?OOP is wonderful in my opinion. When you first look into it you say to yourself, "Ok, I could have done this before without OOP and its complicated syntax. No thanks!". But now I use objects for everything I can. It makes updating easier and programming simpler. The idea of OOP is that you can create an infinite amount of objects (up too your max memory anyway) with their own personal variables. Lets say you have a "People" program. You want to record the people in your life for whatever reason.Family:Steven - FatherJane - MotherJames - BrotherJordon - BrotherMaggie - SisterFriends:KirkJimmyHaleyNow WITHOUT OOP you might have to do something like this-
janeHair = "black";janeEyeColor = "brown";function getJaneHairColor(){   return janeHair;}

That's going to get tedious real quick! Adding new members will be a waste of time. In OOP you would instead be able to do something like this-

Jane = new Person();Jane.setEyeColor("brown");Steven = new Person();Steven.setHairColor("Blonde");alert("Jane has " + Jane.getEyeColor() + " colored eyes while Steven has " + Steven.getHairColor() + " colored hair"):

Organization becomes very easy. It might be overkill for some JS scripts but I use objects in PHP for member login.BUT TO ANSWER YOUR ORIGINAL QUESTIONI've used objects for a js battle system. A game where it got information about a character from a database with php and turned it into a js object where I could manipulate it in a battle scene.

OOP is great, but at some small task, it could be an over kill and hence not so efficient, for example, a 5 pages website, all you want are rollover buttons and dynamic body content, so all you need is an ajax function to load content from an xml file while using another function to give all the buttons their corresponding rollovers. You accomplish everything you wanted in two functions.
Link to comment
Share on other sites

Another point to ponder. Unless we're dealing with HUGE chunks of data or HUGE numbers of iterations, 20 lines of JavaScript will execute virtually as fast as 10 lines of code. They will also transfer virtually as fast. So these are not useful measurements of efficiency. Programmer efficiency, however, is a good measure. And if an OOP guy gets the job done as fast as a procedural guy, then the rest doesn't matter. Which is what's going to happen, since the OOP guy thinks faster in OOP than he does any other way.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...