继承私有变量 [英] inherited private variables

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

问题描述

我(我想)我想出了一个我迄今为止在任何

出版物中都没有看到的模式,我想要一些反馈。基本上,我是

寻找继承私人功能的方法,我想出了

这个:


//基础私有对象构造函数


函数priv(){

this.a = 1;

this.b = 2;

}


//继承自私有对象的私有对象构造函数


函数priv2(){

this.c = 3;

this.d = 4;

}

priv2.prototype = new priv() ;


//在私有命名空间中使用私有对象的构造函数


函数ob(){

var _ = new priv2();

返回{

go:function(){

alert(_。a);

}

}

}


var test = new ob();


//返回1


test.go();


//只有go()是公开的


测试;


---------------------------- ---------------


首先,这是一个知道n模式?如果是这样,抱歉多余的

信息。如果没有,你可以看到任何缺点吗?我认为

的优势在于,继承的对象也可以继承

私有函数和属性供使用。


欢迎任何评论。

I (think) that I''ve come up with a pattern that I haven''t seen in any
publications so far and I would like some feedback. Basically, I was
looking for a way to inherit private functions and I came up with
this:

//base private object constructor

function priv(){
this.a = 1;
this.b = 2;
}

//private object constructor that inherits from base private object

function priv2(){
this.c = 3;
this.d = 4;
}
priv2.prototype = new priv();

//constructor that uses private object in private namespace

function ob(){
var _ = new priv2();
return {
go:function(){
alert(_.a);
}
}
}

var test = new ob();

//returns 1

test.go();

//only go() is public

test;

-------------------------------------------

First of all, is this a known pattern? If so, sorry for the redundant
information. If not, are there any drawbacks that you can see? The
advantage, as I see it, is that inherited objects can also inherit
private functions and properties for use.

Any comments would be welcome.

推荐答案

PragueExpat写道:
PragueExpat wrote:

我(想)我想出了一个我迄今为止在任何

出版物中都没有看到的模式,我想要一些反馈。基本上,我是

寻找继承私人功能的方法,我想出了

这个:


//基础私有对象构造函数


函数priv(){

this.a = 1;

this.b = 2;

}
I (think) that I''ve come up with a pattern that I haven''t seen in any
publications so far and I would like some feedback. Basically, I was
looking for a way to inherit private functions and I came up with
this:

//base private object constructor

function priv(){
this.a = 1;
this.b = 2;
}



[snip]


a和b这里不是私有变量。他们是对象的成员

所以对任何有权访问该对象的人都是公开的。


结果,你的模式的其余部分没有''真的显示任何东西

你根本没有任何私人成员(除了,或许是

''_'',但这是私有的你的最终目标,所以访问它不是一个很好的成就。

[snip]

a and b are not private variables here. They are members of the object
so the are public to anyone with access to that object.

As a result, the rest of your pattern doesn''t really show anything as
you don''t have any private members in your code at all (except, perhaps
''_'', but that is private in your final object, so accessing it is not a
big achievement.


6月9日,9:12 pm,Dan Rumney< danrum ... @ warpmail.netwrote:
On Jun 9, 9:12 pm, Dan Rumney <danrum...@warpmail.netwrote:

PragueExpat写道:
PragueExpat wrote:

我(想)我我想出了一个我迄今为止在任何

出版物中都没有看到的模式,我想要一些反馈。基本上,我是

寻找一种方式继承私有函数,我想出了

这个:
I (think) that I''ve come up with a pattern that I haven''t seen in any
publications so far and I would like some feedback. Basically, I was
looking for a way to inherit private functions and I came up with
this:


//基本私有对象构造函数
//base private object constructor


function priv(){

this.a = 1;

this.b = 2;

}
function priv(){
this.a = 1;
this.b = 2;
}



[snip]


a和b这里不是私有变量。他们是对象的成员

所以对任何有权访问该对象的人都是公开的。


结果,你的模式的其余部分没有''真的显示任何东西

你根本没有任何私人成员(除了,或许是

''_'',但这是私有的你的最终对象,所以访问它不是一个很好的成就。


[snip]

a and b are not private variables here. They are members of the object
so the are public to anyone with access to that object.

As a result, the rest of your pattern doesn''t really show anything as
you don''t have any private members in your code at all (except, perhaps
''_'', but that is private in your final object, so accessing it is not a
big achievement.



我不太确定。构造函数priv和priv2不习惯

创建任何公共对象。用于创建一个对象

(_),这是对象''test''的私有对象。如果你可以看到



函数ob中的特权方法之外访问a,b,c或d的方法,请告诉我如何。


I''m not so sure. The constructors priv and priv2 are not used to
create any public objects. The are used to create exactly one object
( _ ), which is private to the object ''test''. If you can see a way to
access a, b, c or d from outside of a privileged method in the
function ob, please show me how.


PragueExpat< ri ******** @ gmail.comwrites:
PragueExpat <ri********@gmail.comwrites:

我不是这样确定。构造函数priv和priv2不习惯

cr eate任何公共对象。用于创建一个对象

(_),这是对象''test''的私有对象。如果你能看到



函数ob中的特权方法之外访问a,b,c或d的方法,请告诉我如何操作。
I''m not so sure. The constructors priv and priv2 are not used to
create any public objects. The are used to create exactly one object
( _ ), which is private to the object ''test''. If you can see a way to
access a, b, c or d from outside of a privileged method in the
function ob, please show me how.



priv2.prototype.a


priv2.prototype.b


当然,你*可以*在实例化你需要的所有

之后删除priv2.prototype。


在javascript中获得真正私有属性的唯一理智方法是* b $ b根本不使用属性而是使用闭包。但这几乎不是新闻。


-

Joost Diepenmaat |博客: http://joost.zeekat.nl/ |工作: http://zeekat.nl/

priv2.prototype.a

priv2.prototype.b

Ofcourse, you *could* delete priv2.prototype after instantiating all
the priv2 objects you need.

The only sane way to get really private properties in javascript is to
not use properties at all and use closures instead. But that''s hardly
news.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

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

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