Javascript 如何在js中存储多个函数的dom变量?

Javascript 如何在js中存储多个函数的dom变量?,javascript,mootools,Javascript,Mootools,有几个函数必须获取dom$$('.box')[0], 我不想让box成为glabal变量,也不想让js寻找 每一次都是这样。当用户不运行这些函数时,我不想运行$$('.box')[0]。 如何存储box变量? /* ------- 有几个函数必须获取dom$$('.box')[0], 我不想让box成为glabal变量,也不想让js寻找 每一次都是这样。当用户不运行这些函数时,我不想运行$$('.box')[0]。 如何存储box变量? ------- */ 函数a(){ 变量框=$$('

有几个函数必须获取dom$$('.box')[0], 我不想让box成为glabal变量,也不想让js寻找 每一次都是这样。当用户不运行这些函数时,我不想运行$$('.box')[0]。 如何存储box变量?


/* ------- 
有几个函数必须获取dom$$('.box')[0],
我不想让box成为glabal变量,也不想让js寻找
每一次都是这样。当用户不运行这些函数时,我不想运行$$('.box')[0]。
如何存储box变量?
------- */
函数a(){
变量框=$$('.box')[0];
//...
}
函数b(){
变量框=$$('.box')[0];
//...
}
//...

我不知道这是否有帮助,但您可以将它的获取分离到一个函数中,只允许执行一次。我忘记了这个函数是从哪里来的,但它只允许函数运行一次(每隔调用一次,只返回值)非常有用:

所以我会像这样使用它:

var getBox = once(function () {
    return $$('.box')[0];
});
当您想要获取元素时,只需始终使用
getBox()
。只有第一次调用它时,才会搜索DOM。在那之后,它每次都会把盒子还给你


虽然这可能会“有帮助”,但它与创建一个全局变量一样好,因此我不确定您期望的解决方案是什么。

我不知道这是否有帮助,但您可以将它的获取分离到一个函数中,只允许它执行一次。我忘记了这个函数是从哪里来的,但它只允许函数运行一次(每隔调用一次,只返回值)非常有用:

所以我会像这样使用它:

var getBox = once(function () {
    return $$('.box')[0];
});
当您想要获取元素时,只需始终使用
getBox()
。只有第一次调用它时,才会搜索DOM。在那之后,它每次都会把盒子还给你


虽然这可能“有帮助”,但它与创建一个全局变量一样好,因此我不能确切地确定解决方案是什么。

将您的
框声明为不在函数范围内的
变量

var box = "";

function a(){
      if(box != "" || box != undefined || box != 'undefined')
        alert(box +" from a");
      else
        box = $$('.box')[0];       
    }

function b(){
      if(box != "" || box != undefined || box != 'undefined')
        alert(box + " from b");
      else
        box = $$('.box')[0];        
    }

将您的
变量声明在函数范围之外

var box = "";

function a(){
      if(box != "" || box != undefined || box != 'undefined')
        alert(box +" from a");
      else
        box = $$('.box')[0];       
    }

function b(){
      if(box != "" || box != undefined || box != 'undefined')
        alert(box + " from b");
      else
        box = $$('.box')[0];        
    }

尝试创建pseusdo命名空间,并在其中创建应用程序缓存

var mySpace = (function(){
    var appcache = {};
    function a(){
        var box = appcache.box0 
                   || (appcache.box0 = $$('.box')[0], appcache.box0);
        //...
    }
    function b(){
        var box = appcache.box0 
                   || (appcache.box0 = $$('.box')[0], appcache.box0);
        //...
    }
    return {a: a, b: b};
}());
// usage: you can call a or b like
mySpace.a();
mySpace.b();

尝试创建pseusdo命名空间,并在其中创建应用程序缓存

var mySpace = (function(){
    var appcache = {};
    function a(){
        var box = appcache.box0 
                   || (appcache.box0 = $$('.box')[0], appcache.box0);
        //...
    }
    function b(){
        var box = appcache.box0 
                   || (appcache.box0 = $$('.box')[0], appcache.box0);
        //...
    }
    return {a: a, b: b};
}());
// usage: you can call a or b like
mySpace.a();
mySpace.b();

如果您担心性能,只需给您的盒子一个id,然后使用

var  element = null;

function myFun(){
if(!element)
    element = $('#myId');
//Do logic
}

如果您担心性能,只需给您的盒子一个id,然后使用

var  element = null;

function myFun(){
if(!element)
    element = $('#myId');
//Do logic
}

你真的应该用闭包

(function(scope){

    var box = box document.getElement('.box'); // same as $$()[0], returns first match

    scope.a = function(){
        return box;
    };

    scope.b = function(){
        box;
    };

}(this)); // whatever object, eg, window or a namespace

box; // reference error.
this.a(); // box element object
盒子将保持私有且静态,即不再刷新

您可以这样做,以便在需要时将其引用和缓存一次:

(function(scope){
    var box;

    scope.a = function(){
        box = box || document.getElement('.box');
        return box;
    };

    scope.b = function(){
        // alt syntax;
        box || (box = document.getElement('.box'));
        return box;
    };

}(this)); // whatever object, eg, window or a namespace

这样,调用任何一个方法都将缓存该框,并使其可用于闭包中的任何方法。

您应该真正使用闭包

(function(scope){

    var box = box document.getElement('.box'); // same as $$()[0], returns first match

    scope.a = function(){
        return box;
    };

    scope.b = function(){
        box;
    };

}(this)); // whatever object, eg, window or a namespace

box; // reference error.
this.a(); // box element object
盒子将保持私有且静态,即不再刷新

您可以这样做,以便在需要时将其引用和缓存一次:

(function(scope){
    var box;

    scope.a = function(){
        box = box || document.getElement('.box');
        return box;
    };

    scope.b = function(){
        // alt syntax;
        box || (box = document.getElement('.box'));
        return box;
    };

}(this)); // whatever object, eg, window or a namespace
这样,调用任一方法都将缓存该框,并使其可用于闭包中的任何方法