小编nov*_*eek的帖子

在jQuery中访问私有变量的问题,如可链接的设计模式

我正在尝试创建模仿jQuery设计模式的自定义工具箱.基本上,这个想法有点来源于这篇文章:用于处理私有函数的jQuery插件设计模式(通用实践?)(检查"David"给出的答案).

所以这是我的工具箱功能:

(function(window){
    var mySpace=function(){
        return new PrivateSpace();
    }

    var PrivateSpace=function(){
        var testCache={};
    };    

    PrivateSpace.prototype={
        init:function(){
            console.log('init this:', this);
            return this;
        },
        ajax:function(){
            console.log('make ajax calls here');
            return this;
        },
        cache:function(key,selector){
            console.log('cache selectors here');
            testCache[key]=selector;
            console.log('cached selector: ',testCache);
            return this;
        }
    }
    window.hmis=window.m$=mySpace();
})(window)
Run Code Online (Sandbox Code Playgroud)

现在,如果我执行此函数,如:

  console.log(m$.cache('firstname','#FirstNameTextbox'));
Run Code Online (Sandbox Code Playgroud)

我收到错误'testCache'没有定义.我无法在原型的缓存功能中访问变量"testCache".我该如何访问它?基本上,我想要做的是,我想将所有jQuery选择器缓存到一个对象中,并在将来使用此对象.

javascript jquery

3
推荐指数
1
解决办法
1613
查看次数

标签 统计

javascript ×1

jquery ×1