有没有办法将字段设置在子对象范围内,同时可以从父对象访问?
我正在用 JavaScript 进行 OOP 的一些体验。我的目标是拥有一个父对象,其中包含从该父对象继承的几个其他对象通用的方法。问题是,我希望父对象的方法能够读取子对象的字段。
我使用以下函数进行继承:
Function.prototype.inherits=function(obj){this.prototype=new obj();}
这些是一些示例对象:
function Genetic(c){
this.code=c;
}
//My 'parent object':
function Animal(){
this.getCode=function(){
return(genetic.code);
}
}
g=new Genetic('test');
function Dog(){
genetic=g;
}
Dog.inherits(Animal);
g=new Genetic('foo');
function Cat(){
genetic=g;
}
Cat.inherits(Animal);
d=new Dog();
c=new Cat();
现在,我期望 d.getCode()
返回 'test'
和 c.getCode ()
返回'foo'
。问题是,两者都返回'foo'
。变量generic
位于Animal
范围内,而不位于Dog
/Cat
范围内。这意味着每当我创建一个继承自 Animal
的新对象时,Genetic
变量都会被覆盖。证明:
function Bla(){}
Bla.inherits(Animal);
bla=new Bla()
bla.getCode() //Returns 'foo'
我可以使用 var 将 Genetic
变量设置为 Dog
和 Cat
的私有变量:
function Dog(){
var genetic=g;
}
问题是,因为 Genetic< /code> 现在是
Dog
私有的,它不能被 Animal
对象访问,使得整个继承变得毫无意义。
你有什么办法解决这个问题吗?
编辑:另外,我希望 gentic
是私有的,这样就不能在 Dog
/Cat
实例中修改它。
I'm doing some experiences with OOP in JavaScript. My goal is to have a parent object which holds methods common to several other objects, which inherit from that parent object. Thing is, I want the parent objects' methods to be able to read the childrens' fields.
I use the following function for inheritance:
Function.prototype.inherits=function(obj){this.prototype=new obj();}
These are some example objects:
function Genetic(c){
this.code=c;
}
//My 'parent object':
function Animal(){
this.getCode=function(){
return(genetic.code);
}
}
g=new Genetic('test');
function Dog(){
genetic=g;
}
Dog.inherits(Animal);
g=new Genetic('foo');
function Cat(){
genetic=g;
}
Cat.inherits(Animal);
d=new Dog();
c=new Cat();
Now, I expect d.getCode()
to return 'test'
, and c.getCode()
to return 'foo'
. Problem is, both return 'foo'
. The variable genetic
is in the Animal
scope, and not in the Dog
/Cat
scope. Meaning that whenever I create a new object that inherits from Animal
, the genetic
variable will be overridden. Proof:
function Bla(){}
Bla.inherits(Animal);
bla=new Bla()
bla.getCode() //Returns 'foo'
I can set the genetic
variable to being a private variable of Dog
and Cat
with var:
function Dog(){
var genetic=g;
}
Problem is, since genetic
is now private to Dog
, it can't be accessed by the Animal
object, rendering the whole inheritance pointless.
Do you see any way to solve that?
EDIT: Also, I want gentic
to be private, so that it can't be modified in Dog
/Cat
instances.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,
遗传
是全球的。整个应用程序中只存在一个遗传
变量。使其成为对象的属性。此外,更好的继承方式如下:
然后您可以让父构造函数接受参数,而不必重复代码:
我建议 正确记录你的代码,宁愿遵循清晰的原型/继承链,也不要尝试做一些该语言不适合的事情。
但是,使用上面给出的
inherits
函数,您可以这样做:其余代码保持不变。然后,您就拥有了“私有”变量,但代价是每个实例都有自己的 getCode 函数。
编辑:这不会让您在分配给
Dog
或Cat
的任何函数中访问遗传
,除非您还保留对它们的构造函数中的值的引用。No,
genetic
is global. There exists only onegenetic
variable in your whole application. Make it a property of the object.Furthermore, a better way of inheritance is the following:
Then you can have the parent constructor accept arguments and don't have to repeat code:
I would advice to document your code properly and rather follow a clear prototype/inheritance chain than trying to do something the language is not designed for.
However, with the
inherits
function given above, you could do:with the rest of the code staying the same. Then you have your "private" variable at the cost of every instance having its own
getCode
function.Edit: This would not let you access
genetic
in any function assigned toDog
orCat
, unless you also keep a reference to the value in their constructors.