Javascript继承范围问题

发布于 2024-10-19 12:47:21 字数 1411 浏览 1 评论 0原文

我正在尝试使用“伪经典继承”风格来理解 JS 继承。我用谷歌搜索了很多,也读过很多经典文章。我熟悉Java的类结构,并试图理解JS的原型风格。我正在寻找 vanilla JS,因为我想首先了解基础知识。

我有一个简单的父/子测试类设置,需要一些有关范围规则的帮助。

1.) 什么时候在类中定义方法,什么时候在类外定义方法?

2.) 当我使用原型风格创建方法时,如何访问私有变量和私有函数?

function superClass(name){
  this.name = name;
  var privateValue = "I'm Private";
  this.outputPrivate2 = function(){
    alert(privateValue); //works fine
  }      
}

superClass.prototype.outputPrivate = function(){
alert(this.privateValue); //outputs undefined..   
    alert(superClass.prototype.privateValue) //also undefined  
}

3.) 子对象如何调用父对象的私有函数或访问私有变量?

4.) 子对象什么时候应该手动调用父构造函数?

subClass2.prototype = new superClass();                // Define sub-class
subClass2.prototype.constructor = subClass2;

function subClass2(name) {
this.name = name;
this.Bye = function() {
return "Bye from subClass - " + this.name;
}   
this.testm = function(){
superClass.prototype.SomeParentMethod.call(this, "arg1", "arg2");
}

我有三个对象

var parent = new superClass("parent");
var child = new subClass("child1");
parent.outputPrivate(); //undefined
parent.outputPrivate2(); //I'm private
child.outputPrivate(); //undefined
child.outputPrivate2(); //I'm private

,其中 80% 的代码是重复的,因此我创建了一个父对象和三个子对象。子对象具有使用和操作父对象的私有数据的方法。我让它发挥作用的唯一方法是将所有我不喜欢的变量公开。再说一次,我熟悉 Java,所以我可能太努力地让 JS 像 Java 一样工作。

I'm trying to get my head around JS inheritance using the "Pseudo-classical inheritance" style. I've done many Google searches and have read the classic articles. I'm familiar with Java's class structure and am trying to understand JS's prototypal style. I'm looking for vanilla JS since I want to understand the basics first.

I have a simple parent/child test class setup and need some help with the scoping rules.

1.) When do I define methods in the class vs outside of the class?

2.) How do I access private variables and private functions when I create methods using the prototype style?

function superClass(name){
  this.name = name;
  var privateValue = "I'm Private";
  this.outputPrivate2 = function(){
    alert(privateValue); //works fine
  }      
}

superClass.prototype.outputPrivate = function(){
alert(this.privateValue); //outputs undefined..   
    alert(superClass.prototype.privateValue) //also undefined  
}

3.) How Can child objects call private functions or access private variables of the parent?

4.) When should the child object manually call the parent constructor?

subClass2.prototype = new superClass();                // Define sub-class
subClass2.prototype.constructor = subClass2;

function subClass2(name) {
this.name = name;
this.Bye = function() {
return "Bye from subClass - " + this.name;
}   
this.testm = function(){
superClass.prototype.SomeParentMethod.call(this, "arg1", "arg2");
}

}

var parent = new superClass("parent");
var child = new subClass("child1");
parent.outputPrivate(); //undefined
parent.outputPrivate2(); //I'm private
child.outputPrivate(); //undefined
child.outputPrivate2(); //I'm private

I had three objects where 80% of the code was duplicated so I created a parent object and three child objects. The child objects have methods that use and manipulate private data from the parent. The only way I've gotten this to work is make all variables public which I don't like. Again, my familiarity is with Java so I might be trying too hard to make JS work like Java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夢归不見 2024-10-26 12:47:21

您在这里讨论了面向对象 JavaScript 的一些有趣的观点。

1)当你在类中定义一个方法时,每次调用构造函数都会创建一个新函数。如果您使用大量对象,这可能会导致性能问题。当您将方法附加到原型对象时,该函数仅创建一次。

2)但是在构造函数内定义函数的优点是您可以使用“私有”方法/属性。在 Javascript 中,并没有真正的私有变量之类的东西。相反,您正在创建一个包含一些变量的闭包。

如果您需要在构造函数之外使用这些变量,则需要将它们公开。

3)同样的问题。

4)虽然你的问题并不完全清楚,但我会这样做:

function parent(){
    this.a = 1;
}

function child(){
    parent.call(this);
    this.b = 2;
}

obj = new child();
// now obj.a == 1, obj.b == 2

You're addressing some interesting points of object oriented JavaScript here.

1) When you define a method in the class, a new function will be created every time you call the constructor. This may lead to performance issues if you use a lot of objects. When you attach a method to the prototype object, the function is created only once.

2) But the advantage of defining functions inside the constructor is that you can use "private" methods/properties. In Javascript, there isn't really something like a private variable. Instead, you are creating a closure which contains some variables.

If you need to use these variables anyway outside the constructor, you need to make them public.

3) Same problem.

4) Although your question is not totally clear, I would do something like this:

function parent(){
    this.a = 1;
}

function child(){
    parent.call(this);
    this.b = 2;
}

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