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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How do i make this obj more eloquent?

    Hey --

    Right now, I have a variable that I just make a obj that holds functions, variables etc.. but I think it's not very eloquent. Is there a better/more functional way to do it? Class.create? or new obj etc?

    I have access to jquery or prototype.

    This is what I have.. it is a pagination builder ..

    var paginateB = {
    init: function(param){
    this.var = param;
    this.var1 = somevalue1;
    this.var2 = somevalue2;
    this.hashval = $H();
    buildNumbers(this);
    },
    buildNumbers: function(){
    var var3 = somvaluepresetvalue * this.var1;
    //do some stuff
    },
    buildLinks:function(){
    var var4 = somvaluepresetvalue * this.var2;
    //do some stuff
    }
    }

    Besides it not completely working (I think I call the functions before they are declared_.. I just feel I am missing out on some eloquence here..

    Should I create a class, then initialize those vars at the top?

    Then somewhere else in my code, I do this:

    paginateB.init(1);

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    bump.

  • #3
    New Coder
    Join Date
    Mar 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post
    You are pretty close. Just make paginateB a function instead of a variable and omit the init function. You may also have to tweak the other parameters a bit. Then you can call it as var x = new paginateB(param);

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by BrightNail View Post
    Hey --

    Right now, I have a variable that I just make a obj that holds functions, variables etc.. but I think it's not very eloquent. Is there a better/more functional way to do it? Class.create? or new obj etc?

    I have access to jquery or prototype.

    This is what I have.. it is a pagination builder ..

    var paginateB = {
    init: function(param){
    this.var = param;
    this.var1 = somevalue1;
    this.var2 = somevalue2;
    this.hashval = $H();
    buildNumbers(this);
    },
    buildNumbers: function(){
    var var3 = somvaluepresetvalue * this.var1;
    //do some stuff
    },
    buildLinks:function(){
    var var4 = somvaluepresetvalue * this.var2;
    //do some stuff
    }
    }

    Besides it not completely working (I think I call the functions before they are declared_.. I just feel I am missing out on some eloquence here..

    Should I create a class, then initialize those vars at the top?

    Then somewhere else in my code, I do this:

    paginateB.init(1);
    You havn't given us anything
    we can work with here but,
    this.var = param;
    won't do , maybe
    this.var0 = param;
    instead .
    buildNumbers(this);
    I think should be
    this.buildNumbers(this);
    samewith buildLinks
    Last edited by DaveyErwin; 10-05-2011 at 06:29 PM.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Well, I just put the structure of what I have.. those "vars" and such are just placeholder. What I was driving at was the var builderP and the functions within it.

    I was just curious if it would be more eloquent, beneficial to do soemthing like:

    builderP = function(){
    //everything in here
    buildNumbers: function(){
    var var3 = somvaluepresetvalue * this.var1;
    //do some stuff
    },
    buildLinks:function(){
    var var4 = somvaluepresetvalue * this.var2;
    //do some stuff
    }

    }


    or

    builderP = Class.create({
    initialize: Object.exted({
    this.var0 etc..
    this.var1 etc..
    this.var2
    }),
    buildNumbers: function(){
    var var3 = somvaluepresetvalue * this.var1;
    //do some stuff
    },
    buildLinks:function(){
    var var4 = somvaluepresetvalue * this.var2;
    //do some stuff
    }


    })

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Well I have ie7 here and window.Class is undefined .

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DaveyErwin View Post
    Well I have ie7 here and window.Class is undefined .
    I have access to prototype/jquery and the Class.create method is extended via that.

  • #8
    New Coder
    Join Date
    Mar 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post
    Have you tried what I just posted above? That should work for you..

  • #9
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    If you feel an object is your
    best solution the make an Object.


    Code:
    myObject={myProperty:"default",myMethod:function(){alert(this.myProperty)}}
    myObject.myProperty = "prop1";
    If you need more than one
    instance of your object make
    a Constructor...

    Code:
    MyConstructor = function(prop){
     var property = prop || "default";
     this.myProperty = property;
     this.myMethod = function(){
      alert(property);
      alert(this.myProperty);
     }
    }
    myObj1 = new MyConstructor("prop1");
    myObj2 = new MyConstructor();

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thank you everyone who has responded.

    I think part of the issue is what is best to use?

    Right now, I just have a function with a few functions within in it.
    But not sure if that is too "old school".

    I am making a pagination for a bottom of a json search.. so I am not sure I need an object or not. Every time someone hits a num or prev/next I will redraw do some "pagination nav" workings etc..

    So, do I need an object or keep it in functions only? I pass in the "link number"..ie prev 1 2 3 4 next, etc.. so if someone clicks 3, I do this.

    buildpagi(3)

    but not sure what is most eloquent.

    Quote Originally Posted by DaveyErwin View Post
    If you feel an object is your
    best solution the make an Object.


    Code:
    myObject={myProperty:"default",myMethod:function(){alert(this.myProperty)}}
    myObject.myProperty = "prop1";
    If you need more than one
    instance of your object make
    a Constructor...

    Code:
    MyConstructor = function(prop){
     var property = prop || "default";
     this.myProperty = property;
     this.myMethod = function(){
      alert(property);
      alert(this.myProperty);
     }
    }
    myObj1 = new MyConstructor("prop1");
    myObj2 = new MyConstructor();


  •  

    Posting Permissions

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