JavaScrit関数のクロージャスコープに苦労しています。以下の関数は、異なる画像で3つの見本を作成し(これは機能します)、これらをクリックすると、スタイルシートを切り替える必要があります。
switchTheme
問題は、ステップスルーtheme
すると最初の関数の変数が変更されていることを示していても、同じオブジェクトが関数に渡されることです。
var switcherConfig = {
themes:
{
'Orangeness': {
folder: 'ui-lightness'
},
'Red Matter': {
folder: 'blitzer'
},
'Flubber': {
folder: 'south-street'
}
}
}
function createThemeSwitcher(placeholderSelector) {
for (var themeName in switcherConfig.themes) {
var theme = switcherConfig.themes[themeName];
var anchor = $('<a/>')
//.text(theme.title)
.attr('title', theme.title)
.attr('href', '#')
.on('click', function () { switchTheme(theme); })
// append to DOM etc
}
}
function switchTheme(theme) {
var themeDirectory = switcherConfig.baseDirectory + '/' + theme.folder + '/';
// 'theme' variable is always the last in my 'themes' config object
}