有没有办法将字段设置在子对象范围内,同时可以从父对象访问?

发布于 2024-12-03 18:45:30 字数 1386 浏览 1 评论 0原文

我正在用 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 变量设置为 DogCat 的私有变量:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魄砕の薆 2024-12-10 18:45:30

变量“遗传”在动物范围内,而不在狗/猫范围内。

不,遗传全球的。整个应用程序中只存在一个遗传变量。使其成为对象的属性。

此外,更好的继承方式如下:

function inherits(Child, Parent) {
    var Tmp = function(){};
    TMP.prototype = Parent.prototype;
    Child.prototype = new Tmp();
    Child.prototype.constructor = Child;
}

然后您可以让父构造函数接受参数,而不必重复代码:

//My 'parent object':
function Animal(g){
    this.genetic = g;
}

Animal.prototype.getCode = function() {
    return this.genetic.code;
}

function Dog(){
    Animal.apply(this, arguments);
}
inherits(Dog, Animal);

function Cat(){
    Animal.apply(this, arguments);
}
inherits(Cat, Animal);

var d = new Dog(new Genetic('test'));
var c = new Cat(new Genetic('foo'));

我建议 正确记录你的代码,宁愿遵循清晰的原型/继承链,也不要尝试做一些该语言不适合的事情。

但是,使用上面给出的 inherits 函数,您可以这样做:

function Animal(g){
    var genetic = g

    this.getCode = function(){
        return genetic.code ;
    }
}

其余代码保持不变。然后,您就拥有了“私有”变量,但代价是每个实例都有自己的 getCode 函数。

编辑:这不会让您在分配给 DogCat 的任何函数中访问遗传,除非您还保留对它们的构造函数中的值的引用。

The variable 'genetic' is in the Animal scope, and not in the Dog/Cat scope.

No, genetic is global. There exists only one genetic variable in your whole application. Make it a property of the object.

Furthermore, a better way of inheritance is the following:

function inherits(Child, Parent) {
    var Tmp = function(){};
    TMP.prototype = Parent.prototype;
    Child.prototype = new Tmp();
    Child.prototype.constructor = Child;
}

Then you can have the parent constructor accept arguments and don't have to repeat code:

//My 'parent object':
function Animal(g){
    this.genetic = g;
}

Animal.prototype.getCode = function() {
    return this.genetic.code;
}

function Dog(){
    Animal.apply(this, arguments);
}
inherits(Dog, Animal);

function Cat(){
    Animal.apply(this, arguments);
}
inherits(Cat, Animal);

var d = new Dog(new Genetic('test'));
var c = new Cat(new Genetic('foo'));

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:

function Animal(g){
    var genetic = g

    this.getCode = function(){
        return genetic.code ;
    }
}

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 to Dog or Cat, unless you also keep a reference to the value in their constructors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文