在多个< script>中共享JS变量块 [英] Sharing JS variables in multiple <script> blocks

查看:92
本文介绍了在多个< script>中共享JS变量块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个CodeIgniter应用程序。

I'm working on a CodeIgniter application.

我有一个视图,让我们称之为Calendar,它有一个JS / jQuery ; script> 阻止。看起来像这样:

I have a view, let's call it Calendar, which has a JS/jQuery <script> block in it. Looks like this:

    $(document).ready(function()    {

        $("#day_list").fadeIn(600);

        // init
        var current_month = <?= $init['current_month']; ?>;
        var current_year = <?= $init['current_year'] ?>;

        // previous, next month ajax call
        $(".previous, .next").click(function(event) {
            // do stuff to these variables, then ajax call.
            $.ajax({
                // ajax
            });
        });
    });

在另一个视图中,我的脚注,我有另一个脚本块,应该使用相同的变量(current_month和今年)。然而,它不知道它们的存在。将这些变量从第一个< script> 块传递到另一个块的最快,最简单的方法是什么?我不能把两个块在一起,因为我的应用程序的构建方式。我应该为它写一个函数,它获取和返回这些值(我应该怎么做?我是一个新手)还是有更容易的方法?

In another view, my footer, I have another script block which should use the same variables (current_month and current_year). However, it doesn't know about their existence. What would be the quickest, and easiest, way to pass these variables from the first <script> block to the other? I can't put the two blocks together because of the way my application is build. Should I just write a function for it which gets and returns these values (and how should I do this? I'm such a newbie) or is there an easier way?

非常感谢!

推荐答案

在JavaScript中学习命名空间变量非常重要。范围事关重要,很重要。现在因为你在使用var关键字,你的东西将在本地范围。

It's really important to learn to namespace your variables in JavaScript. Scope matters, and it matters a lot. Right now because you're using the "var" keyword, your stuff will be in the local scope.

这里的一些其他答案说,你应该将他们移动全局范围。这是有效的,除非有东西无意中覆盖它们。我非常不同意这种方法,全局范围的变量是JavaScript中的不良做法。

Some of the other answers here say that you should move them into the global scope. That works, unless something else overwrites them unintentionally. I highly disagree with this approach, globally scoped variables are bad practice in JavaScript.

命名空间的工作方式如下:

Namespacing works like this:

var foo = foo || {} //Use existing foo or create an empty object.
foo.bar = foo.bar || {}
foo.bar.baz = foo.bar.baz || {}

这可能看起来像更多的工作,但它也保护你的变量。

This may seem like a lot more work, but it also PROTECTS YOUR VARIABLES.

还添加一个简单的命名空间函数,安全地命名空间一切对窗口对象。

You can also add a simple namespacing function that safely namespaces everything against the window object. (I cribbed this from somewhere ages ago, and I think I modified it a little but maybe didn't).

把这放在你的应用程序的顶部,你可以命名空间的东西用$ .namespace(myapp.mydata),然后说myapp.mydata.currentYear = ...

Put this at the top of your app and you can namespace stuff with $.namespace("myapp.mydata") and then say myapp.mydata.currentYear = ...

$.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=a[i].split(".");
        o=window;
        for (j=0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }
    return o;
};

此外,如果你是新的,或者想要得到核心,我建议阅读JavaScript的好零件by Crockford。

Also, if you're new, or want to get hardcore, I recommend reading JavaScript the Good Parts by Crockford.

这篇关于在多个&lt; script&gt;中共享JS变量块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆