简体   繁体   English

JS Revealing Module Pattern——通过公共方法访问私有变量

[英]JS Revealing Module Pattern - access private variables via public methods

i created txtModule module using Revealing Module Pattern with jquery,我使用 Revealing Module Pattern 和 jquery 创建了txtModule模块,

i want print value of a input tag to a console.. for that exposed test method as public as shown below code我想将输入标签的值打印到控制台.. 对于公开的公开测试方法,如下代码所示

        var txtModule = (function(window,$){

            var txt = {
                topics:{},
                test:function(){
                    console.log(this.input.val());    
                },
                _init:function(){
                    this._cacheDom();                        
                },
                _cacheDom:function(){  
                    this.input = $("input#c_input");                                     
                },
            }
            
            txt._init();
            
            return {                
                test : txt.test,
            }
        });
    
          
        var v = txtModule(window,$);
        v.test();

when execute test public method when try to access this.input variable there is a error appear as below当尝试访问this.input变量时执行测试公共方法时出现如下错误

Uncaught TypeError: Cannot read properties of undefined (reading 'val')

I want to know how can correctly expose test method to outside to access this.input我想知道如何正确地将测试方法暴露给外部以访问this.input

Calling txt._init() populates the txt object with an input property - but then when you do调用txt._init()使用input属性填充txt object - 但是当你这样做时

v.test();

later, you can see that the left side of the .稍后,您可以看到. is v - which is the object returned at the endv - 这是最后返回的 object

        return {                
            test : txt.test,
        }

and not the txt object. When you do v.test() , the this that the test function sees is the object that only has the test property.而不是txt object。当您执行v.test()时, test function 看到的this是仅具有test属性的 object。

It depends on whether you want the input to be visible externally or not - if not, then refer to txt , otherwise create a variable for the returned object and refer to it.这取决于您是否希望输入在外部可见 - 如果不可见,则引用txt ,否则为返回的 object 创建一个变量并引用它。

var txtModule = (function(window,$){
    var txt = {
        topics:{},
        test:function(){
            console.log(this.input.val());    
        },
        _init:function(){
            this._cacheDom();                        
        },
        _cacheDom:function(){  
            returnedObj.input = $("input#c_input");                                     
        },
    }

    const returnedObj = {                
        test : txt.test,
    };
    txt._init();
    return returnedObj;
});

var v = txtModule(window,$);
v.test();

But this is somewhat convoluted - having two different objects that collect somewhat similar key-value pairs makes things confusing.但这有点令人费解——让两个不同的对象收集有些相似的键值对会让事情变得混乱。 Consider if using only a single one instead would help, eg:考虑一下是否只使用一个会有所帮助,例如:

const txtModule = (function (window, $) {
    const input = $("input#c_input");
    const test = () => {
        console.log(input.val());
    };
    return { test };
});

const v = txtModule(window, $);
v.test();

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

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