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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Prototype - Class creation and organization

    Hello all. I've made classes, in prototype, for various projects and have now come to a point where I am a bit stuck. In the past I had trouble with scoping issues and having to bind the current object to the this keyword, so I can use it in 'lexical closures'(not sure if that's the correct terminology).

    Example:
    Code:
    var Example = Class.create({
        initialize : function() {
            this.newElement = $(something);
        },
        doStuff: function(){
            //I know this is a ridiculous example, but I'm trying to be concise
            $R(1..10).times(function(){
               alert(this.newElement);
            }.bind(this));
        }
    });
    OK, so notice how I have to bind this to the .times iterator, otherwise I get the window object and not my class.

    Well, I now have quite the large class and would like to organize it better for my own sanity. I was attempting to do something like this:

    Code:
    var Example = Class.create({
        initialize : function() {
            this.newElement = $(something);
            
        },
        
        utilities: {
            doStuff: function(){
                alert(this.newElement);
            }
        }
        
    });
    So, at some point I can call it like this:

    Code:
    var example = new Example();
    example.utilities.doStuff();
    The problem is using the 'this' keyword inside of that doesn't work out.
    I tried this with no luck:
    Code:
    var Example = Class.create({
        initialize : function() {
            this.newElement = $(something);
          
        },
        
        utilities: {
            doStuff: function(){
                alert(this.newElement);
            }
        }.bind(this)
        
    });
    I get a syntax error, at this point.

    Can someone steer me in the right direction? I know I've seen libraries do this before and I think the prototype classing system should have some way to do it.


    [Edit] Sorry, I didn't realize there was a special section for frameworks. If an admin would be so kind as to move it for me, that would be great.
    Last edited by polygone; 07-15-2009 at 09:26 PM.

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    While you don't capitalize it, you're talking about Stephenson's Prototype, yes, not merely about Javascript prototype objects?

    While not a "framework", neither is JQuery, there is a forum for these questions called - Javascript frameworks.

  • #3
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, fside. I am talking about Prototype the framework. ...and yes I didn't see the prototype section, originally.

    Any input?

  • #4
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    http://pastie.org/548222

    Here's a solution that worked for me.


  •  

    Tags for this Thread

    Posting Permissions

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