简体   繁体   English

使用Javascript在一个页面上的多个实例

[英]Multiple instances on one page with Javascript

I'm having trouble figuring out how to handle multiple instances of some javascript functions that I want to run on a page. 我无法弄清楚如何处理我想在页面上运行的一些javascript函数的多个实例。 It's part of a custom analytics project that I'm working on. 它是我正在开发的自定义分析项目的一部分。

I have a function called initData(); 我有一个名为initData()的函数; The function uses a setInterval to call another function that sends a ping to my server every 1000ms. 该函数使用setInterval调用另一个函数,该函数每1000毫秒向我的服务器发送一次ping。

The problem is that I want to be able to have more than one instance of this function on a single page. 问题是我希望能够在一个页面上拥有多个此函数的实例。 My current problem is that as soon as the second instance is called it overwrites all the variables from the first. 我目前的问题是,只要调用第二个实例,它就会覆盖第一个实例中的所有变量。

What's the best way to get around this? 什么是最好的解决方法? Is there a way to make the functions seperate and/or private instances so that they don't interfere with each other? 有没有办法使函数分离和/或私有实例,以便它们不会相互干扰?

By default all variables (and therefore also function declarations) live in the global namespace. 默认情况下,所有变量(以及函数声明)都存在于全局命名空间中。

The only way to introduce a separate namespace in javascript is with a function call. 在javascript中引入单独命名空间的唯一方法是使用函数调用。 This is how you do that: 这是你如何做到的:

 (function () {
     /* your stuff here */
 }());

You wrap your stuff in an anonymous function, and then call it immediately. 你将你的东西包装在一个匿名函数中,然后立即调用它。 This way your functions will be separate, even with the same name. 这样,即使名称相同,您的功能也将是独立的。

Example

So, for instance, if you have code like this: 所以,例如,如果您有这样的代码:

var my, local, data;

function initData() {
    // use my, local and data here.
}

and you have someplace else: 而你还有其他地方:

var some, other, data;

function initData() {
    // use some, other, data here.
}

then one initData will overwrite the other initData . 然后一个initData将覆盖另一个initData However, if you wrap each in its own (function () {}()); 但是,如果你将每个包装在它自己的(function () {}()); then they will be separate. 然后他们会分开。

(function () {
    var my, local, data;

    function initData() {
        // use my, local and data here.
    }
}());


(function () {
    var some, other, data;

    function initData() {
        // use some, other, data here.
    }
}());

Be aware though, that these names are no longer in the global namespace, so they are also not available for use outside of the anonymous function. 但请注意,这些名称不再位于全局名称空间中,因此它们也不能在匿名函数之外使用。

One global 一个全球性的

To control how much and what you expose in the global namespace, it is custom to expose what you need through one global object, usually in all capital letters. 要控制在全局命名空间中显示的内容和内容,可以自定义通过一个全局对象(通常以全部大写字母)显示所需内容。

FOO.module1.initData();
FOO.module2.initData();

You would do this by making sure FOO exists, and if it doesn't: create it. 你可以通过确保FOO存在来做到这一点,如果不存在:创建它。

var FOO = this.FOO || {};

The same for your module namespaces: 模块命名空间也是如此:

FOO.module1 = FOO.module1 || {};

and then inside of the anonymous function, expose what you want. 然后在匿名函数内部,暴露你想要的东西。

Complete example 完整的例子

module1.js: module1.js:

var FOO = this.FOO || {};
FOO.module1 = FOO.module1 || {};

(function () {
    var my, local, data;

    function initData() {
        // use my, local and data here.
    }

    FOO.module1.initData = initData;
}());

module2.js: module2.js:

var FOO = this.FOO || {};
FOO.module2 = FOO.module2 || {};

(function () {
    var some, other, data;

    function initData() {
        // use some, other and data here.
    }

    FOO.module2.initData = initData;
}());

controller.js: controller.js:

FOO.module1.initData();
FOO.module2.initData();

A last tip 最后一个提示

The controller like written is dependent on both module1.js and module2.js and needs to be loaded last. 像写的控制器依赖于module1.jsmodule2.js ,需要最后加载。 That can be avoided using something like jQuery 's document.ready , making it wait for all scripts to load. 这可以通过jQuerydocument.ready类的东西来避免,让它等待所有脚本加载。

jQuery(document).ready(function () {
    FOO.module1.initData();
    FOO.module2.initData();
});

If you're not already using jQuery, you can use a small script like domReady to avoid the bloat. 如果您还没有使用jQuery,可以使用像domReady这样的小脚本来避免膨胀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM