gpt4 book ai didi

javascript - 传递给回调函数的对象未更新

转载 作者:行者123 更新时间:2023-12-03 07:51:29 25 4
gpt4 key购买 nike

我有一个全局对象变量,我将其传递给函数,然后调用另一个函数并将该返回值分配给传递给原始函数的变量。此返回值不会传播到由于某种原因传递给函数的全局对象。

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

initializeOptions 中的 elements var 设置正确,为什么它不更新 optionsElements?我的理解是对象是通过引用传递的,所以在我看来这应该可行。

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;
}

最佳答案

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

是的。尝试将 elements 传递给 getOptionsElements ,在 getOptionsElements 中使用 for..in 循环来设置 elements 的属性:optionsElements,从getOptionsElements返回elements

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)

关于javascript - 传递给回调函数的对象未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34970787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com