JavaScript 模块模式:私有方法如何访问模块的作用域?

发布于 2024-12-22 07:09:34 字数 687 浏览 3 评论 0原文

在实现模块模式时,私有函数如何访问模块的私有属性?我还没有看到任何开发人员这样做的例子。有什么理由不这样做吗?

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();
        }
    };
})();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾冷 2024-12-29 07:09:34

实现模块模式时,私有函数如何访问模块的私有属性?

这些属性在范围内,所以它们“就是这样”

不起作用。

是的,确实如此。

_privateIncrement 无权访问模块的范围。

是的,确实如此。

请参阅以下内容的实时示例

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

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
绳情 2024-12-29 07:09:34

拥有可以访问 this 的私有方法的另一种替代方法是使用 callapply 方法。

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();

当然,如果您计划拥有该对象的多个实例,您可以将 use_restroom 和 buy_food 移动到构造函数之外的原型和 private_stuff 。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文