Javascript 使用窗口对象访问全局用户定义的对象,并使用html中的文本创建额外的特性

Javascript 使用窗口对象访问全局用户定义的对象,并使用html中的文本创建额外的特性,javascript,jquery,Javascript,Jquery,我不太喜欢jquery/javascript,但想就下面的内容征求一些建议。我已经尽可能地减少了。其中大部分是半继承代码,捕获了一堆硬编码的事件。我想通过将对象名放在html中,并在处理时通过jquery访问(按日期、按流行程度)来对它们进行更多的概括。我以字符串形式重试,并通过窗口[current_obj]访问对象。这是一个好方法还是我遗漏了什么?是否有更好的方法引入特异性。谢谢你的建议 <script> var by_date={}; by_date.current_page=1

我不太喜欢jquery/javascript,但想就下面的内容征求一些建议。我已经尽可能地减少了。其中大部分是半继承代码,捕获了一堆硬编码的事件。我想通过将对象名放在html中,并在处理时通过jquery访问(按日期、按流行程度)来对它们进行更多的概括。我以字符串形式重试,并通过窗口[current_obj]访问对象。这是一个好方法还是我遗漏了什么?是否有更好的方法引入特异性。谢谢你的建议

<script>
var by_date={};
by_date.current_page=1;
by_date.per_page=4;

var by_popularity={};
by_popularity.current_page=1;
by_popularity.per_page=4;

$(function(){
    $('.previous.active').live('click',function(){
        window[current_obj].current_page--;
        process(window[current_obj]);
    });
});

function process(game_obj){
   //will process and output new items here
}
</script>

<div class="otherContainer">
  <a class='previous active'>Prev</a><div style="display:none;">by_date</div> | <a class='next'>Next</a><div style="display:none;">by_date</div>
</div>


<div class="topPrevNextContainer">
  <a class='previous active'>Prev</a><div style="display:none;">by_popularity</div> | <a class='next'>Next</a><div style="display:none;">by_popularity</div>
</div>

var by_date={};
按日期。当前页面=1;
截止日期。每页=4;
var由_普及率={};
受欢迎程度。当前页面=1;
受欢迎程度。每页=4;
$(函数(){
$('.previous.active').live('click',function(){
窗口[当前对象]。当前页面--;
过程(窗口[当前对象]);
});
});
功能流程(游戏_obj){
//将在此处处理和输出新项目
}
上一个日期下一个日期
上一次流行|下一次流行|

> p>有些人会认为你的代码会乱扔全局命名空间。在JavaScript中实现有限范围的一种常见技术是定义并立即调用匿名函数

(function() {
    // this function is anonymous and is immediately called
    // variables declared inside this function are not members
    // of the global namespace.

    var options = {};

    options.by_date={};
    options.by_date.current_page=1;
    options.by_date.per_page=4;

    options.by_popularity={};
    options.by_popularity.current_page=1;
    options.by_popularity.per_page=4;

    $(function(){
        $('.previous.active').live('click',function(){
            options[current_obj].current_page--;
            process(options[current_obj]);
        });
    });

    function process(game_obj){
       //will process and output new items here
    }
})();

凉的这是有道理的。所以我想从html文本中获取值并不是一个完整的黑客行为。谢谢