...

View Full Version : How do i make this obj more eloquent?



BrightNail
10-05-2011, 06:54 AM
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);

BrightNail
10-05-2011, 06:56 PM
bump.

coiner
10-05-2011, 07:13 PM
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);

DaveyErwin
10-05-2011, 07:16 PM
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

BrightNail
10-05-2011, 07:23 PM
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
}


})

DaveyErwin
10-05-2011, 07:39 PM
Well I have ie7 here and window.Class is undefined .

BrightNail
10-05-2011, 07:43 PM
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.

coiner
10-05-2011, 08:03 PM
Have you tried what I just posted above? That should work for you..

DaveyErwin
10-05-2011, 09:34 PM
If you feel an object is your
best solution the make an Object.




myObject={myProperty:"default",myMethod:function(){alert(this.myProperty)}}
myObject.myProperty = "prop1";

If you need more than one
instance of your object make
a Constructor...



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();

BrightNail
10-08-2011, 06:20 AM
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.


If you feel an object is your
best solution the make an Object.




myObject={myProperty:"default",myMethod:function(){alert(this.myProperty)}}
myObject.myProperty = "prop1";

If you need more than one
instance of your object make
a Constructor...



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();



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum