I’ve never dabbled in making javascript object like this but I think I have a good reason.
Here is my example:
x = new Something(pallet,['blue','green','orange']);
x.try(['blue']);
I want to make a object that sets up two variables pallet and the colors array.
Then in the try function I want to pass an array but in the function I’d like access to pallet and the colors object. Can someone tell me if this is possible? Perhaps a little demo or tutorial?
Okay, so what we are doing here is creating a closure and then returning the getColors function to the person instantiating the Something object. You can read about closures here and here. Basically, though, you are “closing” over the colors and pallet members and, because getColors exists within the context of Something(), it has access to all of Something’s members. Then, by returning that function back to the caller, we’re allowing them to call that functionality with an object that has “captured” their pallet and color list.
Keep in mind that this was a very, very high level view of closures in general and the Revealing Module pattern specifically. Read the links I included and monkey around on this fiddle I made on JSFiddle. I think you’ll be pleased once you get your head around it.
It seems like you are trying to make a Pallet object, so I created a new fiddle for you.
Here is the code: