Ad

Object Passed To Callback Function Not Updating

- 1 answer

I have a global object variable that I pass to a function, then I call another function and assign that return value to the variable which was passed to the original function. This return value does not propagate to the global object which was passed into the function for some reason.

var DEFAULT_OPTIONS = {...}
var optionsElements = new Object();

function initializeOptions(elements, optionsObject, defaultOptions) {
  elements = getOptionsElements();

  optionsObject = loadOptions(elements, defaultOptions);

  document.getElementById("movie_categories").addEventListener("change", 
      function(event) {
    onCategoryChange(elements, event);
  });

  document.getElementById("tv_categories").addEventListener("change",
      function(event) {
    onCategoryChange(elements, event);
  });
}

initializeOptions(optionsElements, currentOptions, DEFAULT_OPTIONS);

The elements var inside initializeOptions gets set properly, why does it not update optionsElements? My understanding is that Objects are passed by reference, so it seems to me this should work.

function getOptionsElements() {
  options =  {
    "all_movies": document.getElementById("all_movies"),
    "movie_3d": document.getElementById("movie_3d"),
    "movie_480p": document.getElementById("movie_480p"),
    "movie_bd-r": document.getElementById("movie_bd-r"),
    "movie_bd-rip": document.getElementById("movie_bd-rip"),
    "movie_cam": document.getElementById("movie_cam"),
    "movie_dvd-r": document.getElementById("movie_dvd-r"),
    "movie_hd-bluray": document.getElementById("movie_hd-bluray"),
    "movie_kids": document.getElementById("movie_kids"),
    "movie_mp4": document.getElementById("movie_mp4"),
    "movie_non-english": document.getElementById("movie_non-english"),
    "movie_packs": document.getElementById("movie_packs"),
    "movie_web-dl": document.getElementById("movie_web-dl"),
    "movie_xvid": document.getElementById("movie_xvid"),

    "all_tv": document.getElementById("all_tv"),
    "tv_documentaries": document.getElementById("tv_documentaries"),
    "tv_sports": document.getElementById("tv_sports"),
    "tv_480p": document.getElementById("tv_480p"),
    "tv_bd": document.getElementById("tv_bd"),
    "tv_dvd-r": document.getElementById("tv_dvd-r"),
    "tv_dvd-rip": document.getElementById("tv_dvd-rip"),
    "tv_mp4": document.getElementById("tv_mp4"),
    "tv_non-english": document.getElementById("tv_non-english"),
    "tv_packs": document.getElementById("tv_packs"),
    "tv_packs-non-english": document.getElementById("tv_packs-non-english"),
    "tv_sd-x264": document.getElementById("tv_sd-x264"),
    "tv_web-dl": document.getElementById("tv_web-dl"),
    "tv_x264": document.getElementById("tv_x264"),
    "tv_xvid": document.getElementById("tv_xvid"),

    "sort_options": document.getElementById("sort_options")
  }

  return options;
}
Ad

Answer

so after I assign elements = getOptionsElements, elements no longer points to optionsElements, but is now a reference to the object created inside getOptionsElements?

Yes. Try passing elements to getOptionsElements , using for..in loop within getOptionsElements to set properties of elements : optionsElements , return elements from getOptionsElements

var optionsElements = new Object();
function initializeOptions(elements, optionsObject, defaultOptions) {
  elements = getOptionsElements(elements);
  return elements
}

function getOptionsElements(opts) {
  options =  {
    "a":1,
    "b":2,
    "c":3
  };

  for (var prop in options) {
     opts[prop] = options[prop];
  }

  return opts;
};

console.log(initializeOptions(optionsElements), optionsElements)

Ad
source: stackoverflow.com
Ad