Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Javascript OOPs

  1. #1
    Regular Coder
    Join Date
    May 2007
    Location
    Manteca, CA
    Posts
    219
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Javascript OOPs

    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?

  • #2
    TNO
    TNO is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    213
    Thanks
    2
    Thanked 1 Time in 1 Post
    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.

  • #3
    Regular Coder
    Join Date
    May 2007
    Location
    Manteca, CA
    Posts
    219
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,609
    Thanks
    0
    Thanked 645 Times in 635 Posts
    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).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Regular Coder
    Join Date
    May 2007
    Location
    Manteca, CA
    Posts
    219
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,292
    Thanks
    10
    Thanked 583 Times in 564 Posts
    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.

  • #7
    Regular Coder
    Join Date
    May 2007
    Location
    Manteca, CA
    Posts
    219
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •