简体   繁体   English

Javascript:用于揭示模块模式的通用getter / setter

[英]Javascript: Generic getter/setter for revealing module pattern

Basically what I am trying to do is write a re-useable getter/setter to expose variables using the module pattern 基本上我要做的是编写一个可重用的getter / setter来使用模块模式公开变量

// the getter/setter
function pub (variable) {
    return function (value) {
        return (arguments.length ? variable = value : variable);
    }
}

var module = (function () {

    var publicVariable = 0;

    function test () {
        return publicVariable;
    }

    return {
        publicVariable: pub(publicVariable),
        test: test
    }
})();

The desired result is the following: 期望的结果如下:

module.publicVariable();      // 0
module.publicVariable(1);     // 1
module.publicVariable();      // 1
module.test();                // 1

But instead I get: 但相反,我得到:

module.publicVariable();      // 0
module.publicVariable(1);     // 1
module.publicVariable();      // 1
module.test();                // 0

I assume this is because the following line passes the current value of publicVariable to pub , therefore the only closure created is the one inside pub and there is no link to the variable itself. 我假设这是因为以下行将publicVariable的当前值publicVariablepub ,因此创建的唯一闭包是pub内部的一个,并且没有到变量本身的链接。

publicVariable: pub(publicVariable),    // same as "pub(0)"

I know there is no way to pass by reference in javascript. 我知道没有办法在javascript中通过引用传递。 So how else can I accomplish what I am trying to do? 那么我怎样才能完成我想做的事情呢? I do not care whether props are called by function or property. 我不关心道具是由函数还是属性调用。

ie either of the following are fine 即以下任何一种都可以

module.publicVariable = "new value";
module.publicVariable("new value");

I am just getting really tired of writing: 我只是厌倦了写作:

function prop1f (value) {  return (arguments.length ? prop1 = value : prop1); }
function prop2f (value) {  return (arguments.length ? prop2 = value : prop2); }
function prop3f (value) {  return (arguments.length ? prop3 = value : prop3); }

return {
    prop1: prop1f,
    prop2: prop2f,
    prop3: prop3f
}

as this gets unruly quick on large projects with lots of user accessible properties. 因为这对于具有大量用户可访问属性的大型项目来说非常快速。

The problem is that this code: 问题是这段代码:

function pub (variable) {
    return function (value) {
        return (arguments.length ? variable = value : variable);
    }
}

can work perfectly as a getter, but as setter: if you change the parameter itself (variable), that won't affect the item that was fed into the parameter. 可以作为一个getter完美地工作,但作为setter:如果你改变参数本身(变量),那将不会影响输入参数的项目。 But if you change the internals of the parameter, that will propagate back. 但是如果你改变参数的内部,那将会传播回来。

Instead you can use javascript getters / setters : 相反,你可以使用javascript getters / setters

var module = (function () {

    var publicVariable = 0;

    function test () {
        return publicVariable;
    }

    return {
        set publicVariable (value) {
            publicVariable = value;
        },
        get publicVariable () {
            return publicVariable;
        },
        test: test
    }
})();

module.publicVariable = 'New value';
console.log(module.publicVariable);

Demo: 演示:

 var module = (function () { var publicVariable = 0; function test () { return publicVariable; } return { set publicVariable (value) { publicVariable = value; }, get publicVariable () { return publicVariable; }, test: test } })(); module.publicVariable = 'New Value'; console.log(module.publicVariable); console.log(module.test()); 

Another generic alternative: 另一种通用选择:

var module = (function () {

    var publicVariable = 0;

    function test () {
        return publicVariable;
    }

    return {
        //This function can access `publicVariable` !
        publicVariable: function(value) {
            return (arguments.length ? publicVariable = value : publicVariable);
        },
        test: test
    }

})();

module.publicVariable('new Value');
console.log(module.publicVariable());

Demo: 演示:

 var module = (function () { var publicVariable = 0; function test () { return publicVariable; } return { //This function can access `publicVariable` ! publicVariable: function(value) { return (arguments.length ? publicVariable = value : publicVariable); }, test: test } })(); module.publicVariable('new Value'); console.log(module.publicVariable()); 

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

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