最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript module pattern: How do private methods access module's scope? - Stack Overflow

programmeradmin2浏览0评论

When implementing the module pattern, how do private functions access the private properties of the module? I haven't seen any examples where developers do this. Is there any reason not to?

var module = (function(){
    // private property
    var number = 0;

    // private method
    _privateIncrement = function(){
        // how do I access private properties here?
        number++;
    };

    // public api
    return {
        // OK
        getNumber: function(){
             return number;   
        },
        // OK
        incrNumber: function(){
             number++;  
        },
        // Doesn't work. _privateIncrement doesn't have
        // access to the module's scope.
        privateIncrNumber: function(){
            _privateIncrement();
        }
    };
})();

When implementing the module pattern, how do private functions access the private properties of the module? I haven't seen any examples where developers do this. Is there any reason not to?

var module = (function(){
    // private property
    var number = 0;

    // private method
    _privateIncrement = function(){
        // how do I access private properties here?
        number++;
    };

    // public api
    return {
        // OK
        getNumber: function(){
             return number;   
        },
        // OK
        incrNumber: function(){
             number++;  
        },
        // Doesn't work. _privateIncrement doesn't have
        // access to the module's scope.
        privateIncrNumber: function(){
            _privateIncrement();
        }
    };
})();
Share Improve this question asked Dec 20, 2011 at 18:05 ThomasThomas 5,8568 gold badges49 silver badges68 bronze badges 2
  • 7 Works fine: jsfiddle.net/DREKt Although you likely want to precede _privateIncrement with a var declaration. – Dennis Commented Dec 20, 2011 at 18:10
  • if number wasn't bound in the module's closure, and was a part of the object, then you might need to use apply() or call() to invoke the private method in the correct context. _privateIncrement.call(this) – J. Holmes Commented Dec 20, 2011 at 18:15
Add a comment  | 

2 Answers 2

Reset to default 13

When implementing the module pattern, how do private functions access the private properties of the module?

The properties are in scope, so they "just do"

Doesn't work.

Yes, it does.

_privateIncrement doesn't have access to the module's scope.

Yes, it does.

See live example of the following:

var module = (function(){
    // private property
    var number = 0;

    // global method
    _privateIncrement = function(){
        number++;
    };

    // public api
    return {
        // OK
        getNumber: function(){
             return number;   
        },
        // OK
        incrNumber: function(){
             number++;  
        },
        // Does work!
        privateIncrNumber: function(){
            _privateIncrement();
        }
    };
})();

// Show default value
document.body.innerHTML += (module.getNumber());
// Increment
module.privateIncrNumber();
// Show new value
document.body.innerHTML += (module.getNumber());
// Increment (since _privateIncrement was defined as a global!)
_privateIncrement();
// Show new value
document.body.innerHTML += (module.getNumber());

// Output: 012

One alternative to have private methods with access to the this is by using the call or apply methods.

function Restaurant()
{
    this.mongoose = 'beans';
    this.freedom = {bear:'love',a:'12'};

    var myPrivateVar;

    var private_stuff = function()   // Only visible inside Restaurant()
    {
        myPrivateVar = "I can set this here!";
        this.mongoose = 12;
    }

    this.use_restroom = function()   // use_restroom is visible to all
    {
        private_stuff();
    }

    this.buy_food = function()    // buy_food is visible to all
    {
        private_stuff();
    }

    private_stuff.call(this);
}

var bobbys = new Restaurant();

Of course you would move the use_restroom and buy_food to a prototype and private_stuff outside of the constructor if you were planning on having multiple instances of this object.

发布评论

评论列表(0)

  1. 暂无评论