私有实例成员 [英] Private instance members

查看:93
本文介绍了私有实例成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在javascript类中拥有私有实例成员?


函数myObject(){

var private = 1;


this.getPrivate = function(){

返回私人;

}


这个。 incrementPrivate = function(){

private + = 1;

}

}


var a = new myObject();

var b = new myObject();

a.incrementPrivate();


我测试上面的例子,调用b.getPrivate()返回2,所以它显示私有成员只能是静态的
?我知道可以使用" this.private"实现预期的功能

与公共成员但更愿意使变量私有

无法访问类外的代码。

解决方案




Will写道:

是否可以拥有私有实例成员一个javascript类?

函数myObject(){
var private = 1;

this.getPrivate = function(){
返回私有;
}

this.incrementPrivate = function(){
private + = 1;
}
}

var a = new myObject();
var b = new myObject();
a.incrementPrivate();

当我测试上面的例子时,调用b.getPrivate()返回2 ,那么看来私人成员只能是静态的吗?我知道可以使用this.private与公共成员实现预期的功能,但更愿意让变量私有对于课外代码无法访问。




你在使用哪种浏览器?

private是一个保留字我认为因此Netscape会出错并且

根本不应该产生结果。

如果我尝试


函数myObject(){

var private = 1;


this.getPrivate = function(){

返回私密;

}


this.incrementPrivate = function(){

私人+ = 1;

}

}


var a = new myObject();

var b = new myObject();

a.incrementPrivate();

alert(b.getPrivate())


与IE6一起提醒1.

-


Martin Honnen
http://JavaScript.FAQTs.com/


> function myObject(){

var private = 1;

this.getPrivate = function(){
return private;
}

this.incrementPrivate = function(){
private + = 1;
}


var a = new myObject();
var b = new myObject();
a.incrementPrivate();

当我测试上面的例子时,调用b.getPrivate()返回2,所以它出现了
私人成员只能是静态的吗?我知道可以使用this.private与公共成员实现预期的功能,但更愿意让变量私有对于课外代码无法访问。




我通过JSLINT运行它,发现你误用了''私人'',这是一个

保留字。

http://www.crockford.com/javascript/lint.html


很抱歉这个混乱。上一个例子是为了

演示目的而被抛在一起,我应该记得私人

是一个保留字。


问题实际上与具有

原型类的私有实例变量有关。例如:


函数myObject(){

var i = 1;

this.ret = function(){return i;}

this.increment = function(){i + = 1;}

}


myObject2.prototype = new myObject();

函数myObject2(){}


var a = new myObject2();

var b = new myObject2();

a.increment();

alert(b.ret())


在这个例子中,b.ret()返回2我想要的地方1.看起来好像两个对象a和b共享原型的实例

类myObject(),因为每个对象的方法都会改变两个私有变量的值




我发现这种行为有点奇怪,并且想知道是否有我想要的东西,或者是为了使两个对象能够使用

维持变量i。彼此独立,同时保持

i作为私人变量。


Is it possible to have private instance members in a javascript class?

function myObject() {
var private = 1;

this.getPrivate = function() {
return private;
}

this.incrementPrivate = function() {
private += 1;
}
}

var a = new myObject();
var b = new myObject();
a.incrementPrivate();

When I test the example above, calling b.getPrivate() returns 2, so it
appears that private members can only be static? I know that it is
possible to use "this.private" to achieve the intended functionality
with a public member but would prefer to make the variable "private"
inaccessible to code outside the class.

解决方案



Will wrote:

Is it possible to have private instance members in a javascript class?

function myObject() {
var private = 1;

this.getPrivate = function() {
return private;
}

this.incrementPrivate = function() {
private += 1;
}
}

var a = new myObject();
var b = new myObject();
a.incrementPrivate();

When I test the example above, calling b.getPrivate() returns 2, so it
appears that private members can only be static? I know that it is
possible to use "this.private" to achieve the intended functionality
with a public member but would prefer to make the variable "private"
inaccessible to code outside the class.



Which browser are you using?
private is a reserved word I think therefore Netscape gives an error and
shouldn''t yield a result at all.
And if I try

function myObject() {
var private = 1;

this.getPrivate = function() {
return private;
}

this.incrementPrivate = function() {
private += 1;
}
}

var a = new myObject();
var b = new myObject();
a.incrementPrivate();
alert(b.getPrivate())

with IE6 it alerts 1.
--

Martin Honnen
http://JavaScript.FAQTs.com/


> function myObject() {

var private = 1;

this.getPrivate = function() {
return private;
}

this.incrementPrivate = function() {
private += 1;
}
}

var a = new myObject();
var b = new myObject();
a.incrementPrivate();

When I test the example above, calling b.getPrivate() returns 2, so it
appears that private members can only be static? I know that it is
possible to use "this.private" to achieve the intended functionality
with a public member but would prefer to make the variable "private"
inaccessible to code outside the class.



I ran it through JSLINT and it found that you are misusing ''private'', a
reserved word.

http://www.crockford.com/javascript/lint.html


Sorry for the confusion. The previous example was thrown together for
demonstration purposes and I should have remembered about "private"
being a reserved word.

The problem actually relates to private instance variables with a
prototyped class. For example:

function myObject() {
var i = 1;
this.ret = function() {return i;}
this.increment = function() {i += 1;}
}

myObject2.prototype = new myObject();
function myObject2() {}

var a = new myObject2();
var b = new myObject2();
a.increment();
alert(b.ret())

In this example,b.ret() returns 2 where I would expect 1. It appears
as though both objects "a" and "b" share an instance of the prototyped
class "myObject()", because the methods of each object alter the value
of the private variable for both.

I find this behaviour a bit odd and wonder if there is either
something I am missing or a workaround to enable the two objects to
maintain the variable "i" independently of one another whilst keeping
"i" as a private variable.


这篇关于私有实例成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆