...

View Full Version : Javascript OOPs



snoodle
06-19-2007, 12:04 AM
I realize that strictly speaking javascript doesn't support OOP's constructs (like class) but I've been reading about all these methods of coercing javascript to be an object oriented language.

I'm building a javascript table builder and am toying with doing it in one of these suggested ways, but I'm wondering if it makes any sense. The syntax gets a little weird and cumbersome at times and I'm not sure you really get all the gains of a pure OOP's language.

Also, even my procedural javascript files are pretty OOPsy, with a single js file representing a class. The main consideration would be to be able to have more than one table on a page. But then, a table element is already an instance of an object, so I could be passing the element to my "methods" and achieve the same thing. Anybody have any thoughts on the matter? Does it pay to try to make javascript behave like a true OOPs language?

TNO
06-19-2007, 05:50 AM
I've always considered ECMAScript in general to be OOP:

http://www.crockford.com/javascript/

I can understand how it can be confusing if you come from a stricter OOP language, but I consider the dynamics of this language (and its cousins) to be more expressive and powerful in general.

snoodle
06-19-2007, 07:17 AM
im in the midst of a coding frenzy, but i'll be sure and check out ECMAScript. Right now I settled on just using an element as an object and passing in the elem reference to every "pseudo" method. its kind of nice that you can just throw new members into an element, and javascript function pointers are working out nicely... so the code is pretty clean and best of all - its working. in one day i put together a table gizmo that rivals that $400 Active Widget deal.

felgall
06-19-2007, 11:00 AM
JavaScript is a prototype based OOP rather than a class based OOP. All it means is that you have a lot more flexibility than with a normal class based OOP because there is no distinction between classes and objects and you can easily add properties and methods dynamically to an already existing object or group of objects all based on the same prototype (not that you necessarily would, conventional OOP as you would have with regular classes is also possible and more useful in most instances).

snoodle
06-21-2007, 07:06 PM
yeah, i ended up going with the standard javascript oops method, using "prototype" for methods and all that. writing the "classes" themselves is a bit of annoying but using the class is nice.

one question while i'm on the subject...
if you have elements as memebers within your object and it comes to disposal time, i've read you removechild for each element, but do you "delete" the element itself? don't recall seeing anything about that.

so do free up an instance of a class, i would have something like dispose(obj) and dispose would free up each of it's members (that are, themeselves, objects) and then finally "delete" the object instance, itself. does that sound right? and again, do i need to delete element?

rnd me
06-21-2007, 10:05 PM
unless you are not refreshing the page all day, don't worry too much about deleting elements.

JS does it's own internal garbage collection, and if you're avoiding unnecessary closures,( which it sound like you are), you have nothing to worry about.

you could also do all your 'heavy lifting' in a hidden iframe, pushing the results to top.document, and periodically, invisibly refresh the iframe.

snoodle
06-22-2007, 06:12 AM
yeah, i wasn't thinking. i guess as long as it's a child, the browser will take care of cleanup. i'm really putting in the hours these days and making some dumb mistakes. i'll be taking a much needed 2 week break in a few weeks. i think my machine needs a break from me as well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum