I'm confused as to which of these is the most efficient / best practice ways of declaring this object.
Inside the function i'm reusing, but i presume that means it is declared everytime the function is called
function mapVals(ele){
var myObj = {
"chkOne" : "HV1",
"chkTwo" : "HV2",
"chkThree" : "HV3"
}
var thisVal = myObj[ele.id];
//Other code
}
Outside the function but then it is polluting the global namespace
var myObj = {
"chkOne" : "HV1",
"chkTwo" : "HV2",
"chkThree" : "HV3"
}
function mapVals(ele){
var thisVal = myObj[ele.id];
//Other code
}
Or perhaps an encapsulation technique to create a temporary scope? I know the syntax but haven't used it before.
(function(){
var myObj = {
"chkOne" : "HV1",
"chkTwo" : "HV2",
"chkThree" : "HV3"
}
function mapVals(ele){
var thisVal = myObj[ele.id];
//Other code
}
})();
Or anything else I haven't considered?