Javascript 如何在构造函数中设置javascript私有变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6799103/
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
How to set javascript private variables in constructor?
提问by Click Upvote
Say I have a javascript function/class called Foo
and it has a property called bar
. I want the value of bar
to be supplied when the class is instantiated, e.g:
假设我调用了一个 javascript 函数/类Foo
,它有一个名为bar
. 我希望在bar
实例化类时提供的值,例如:
var myFoo = new Foo(5);
would set myFoo.bar
to 5.
将设置myFoo.bar
为 5。
If I make bar
a public variable, then this works, e.g:
如果我创建bar
一个公共变量,那么这有效,例如:
function Foo(bar)
{
this.bar = bar;
}
But if I want to make it private, e.g:
但是,如果我想将其设为私有,例如:
function Foo(bar)
{
var bar;
}
Then how would I set the value of the private variable bar
such that its available to all internal functions of foo
?
那么我将如何设置私有变量的值bar
,使其可用于所有内部函数foo
?
回答by jfriend00
One of the best tutorials on private and protected access in javascript is here: http://javascript.crockford.com/private.html.
关于 javascript 中私有和受保护访问的最佳教程之一在这里:http: //javascript.crockford.com/private.html。
function Foo(a) {
var bar = a; // private instance data
this.getBar = function() {return(bar);} // methods with access to private variable
this.setBar = function(a) {bar = a;}
}
var x = new Foo(3);
var y = x.getBar(); // 3
x.setBar(12);
var z = x.bar; // not allowed (x has no public property named "bar")
回答by Digital Plane
You have to put all functions that need to access the private variable inside the constructor:
您必须将所有需要访问私有变量的函数放在构造函数中:
function Foo(bar)
{
//bar is inside a closure now, only these functions can access it
this.setBar = function() {bar = 5;}
this.getBar = function() {return bar;}
//Other functions
}
var myFoo = new Foo(5);
myFoo.bar; //Undefined, cannot access variable closure
myFoo.getBar(); //Works, returns 5
回答by Paul
function Foo(b)
{
var bar = b;
this.setBar = function(x){
bar = x;
}
this.alertBar = function(){
alert(bar);
}
}
var test = new Foo(10);
alert(test.bar); // Alerts undefined
test.alertBar(); // Alerts 10
回答by marksyzm
One way I can think of is to use a closure that's assigned to a name and returns a new object. You would pass in any arguments to the constructor through the call to the closure. That would end up being something like the following:
我能想到的一种方法是使用分配给名称并返回新对象的闭包。您可以通过调用闭包将任何参数传递给构造函数。这最终将类似于以下内容:
var fooFactory = function (a, b) {
var c = 5,
d = 6,
foo;
foo = function (a, b) {
this.a = a;
this.b = b;
this.bar();
}
foo.prototype.bar = function () {
//do something with c and d
this.c = c + d;
}
foo.prototype.getC = function () {
return c;
}
foo.prototype.getD = function () {
return d;
}
return new foo(a, b);
};
This way, a and b are always declared uniquely. You would then construct your object like so:
这样, a 和 b 总是被唯一声明。然后你可以像这样构造你的对象:
var obj = fooFactory(1, 2);
//obj contains new object: { a: 1, b: 2, c: 11 }
console.log(obj.getC());
//returns 5
回答by Nitin Jadhav
If you are willing to use ES2015 classes,
如果你愿意使用 ES2015 类,
with ESNext, you can use Javascript private variableslike this:
使用 ESNext,您可以像这样使用Javascript 私有变量:
class Foo {
#bar = '';
constructor(val){
this.#bar = val;
}
otherFn(){
console.log(this.#bar);
}
}
Private field #bar is not accessible outside Foo class.
私有字段 #bar 在 Foo 类之外不可访问。
回答by Peter Schweitzer Jr
I recently had a similar issue but wanted to use accessor properties also. Below is a Foo(Bar) example based on what I came up with for a solution. This example is trivial but can easily be expanded upon using more complex get/set functions.
我最近遇到了类似的问题,但也想使用访问器属性。下面是一个基于我想出的解决方案的 Foo(Bar) 示例。这个例子是微不足道的,但可以很容易地扩展使用更复杂的 get/set 函数。
function Foo(Bar){
Object.defineProperty(this,"bar",{get:function(){return Bar},set:function(val){Bar=val}});
}
x=new Foo(3);
y=x.bar; //3
x.bar++; //x.bar==4