将函数中的变量设置为全局:Javascript

时间:2015-05-20 11:02:40

标签: javascript jquery

var per = {Period: {"From":["W" + (moment().format('WW')-11)],"To":["W" + moment().format('WW')]}};;


var show = function (studios, platforms, vodest, sdhd, category, genre) {
var output = {
    Studios: studios,
    Platforms: platforms,
    VODEST: vodest,
    SDHD: sdhd,
    Category: category,
    Genre: genre
};


};
$.extend(output, per);
$('.list').html(JSON.stringify(output));    

当我运行上面的代码时,我得到一个错误 ReferenceError:未定义输出有没有办法可以将输出变量作为全局并在函数外部访问它?

2 个答案:

答案 0 :(得分:0)

var per = {Period: {"From":["W" + (moment().format('WW')-11)],"To":["W" + moment().format('WW')]}};;

var output = {};
var show = function (studios, platforms, vodest, sdhd, category, genre) {
    output['Studios'] = studios,
    output['Platforms']= platforms,
    output['VODEST']= vodest,
    output['SDHD']= sdhd,
    output['Category']= category,
    output['Genre']= genre
};
$.extend(output, per);
$('.list').html(JSON.stringify(output));

答案 1 :(得分:-1)

尝试这个技巧:)

var per = {
    Period: {
        "From":["W" + (moment().format('WW')-11)],
        "To": ["W" + moment().format('WW')]
    }
};

function show (studios, platforms, vodest, sdhd, category, genre) {
    return {
    Studios: studios,
    Platforms: platforms,
    VODEST: vodest,
    SDHD: sdhd,
    Category: category,
    Genre: genre
    };
}

var output = show('s', 'p', 'v', 's', 'c', 'g');
output = $.extend(output, per);

$('.list').html(output);