JavaScript 模块模式:私有方法如何访问模块的范围?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8579885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript module pattern: How do private methods access module's scope?
提问by Thomas
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();
}
};
})();
采纳答案by Quentin
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.
_privateIncrement
无权访问模块的范围。
Yes, it does.
是的,它确实。
See live exampleof 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
回答by Marius Miliunas
One alternative to have private methods with access to the this
is by using the call
or apply
methods.
拥有访问 的私有方法的一种替代方法this
是使用call
或apply
方法。
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.
当然,如果您计划拥有此对象的多个实例,您会将 use_restroom 和 buy_food 移动到构造函数之外的原型和 private_stuff。