2

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.

3
  • The only way that variables and functions defined inside of the scope of a constructor function can be accessed is if they are accessed via a closure Commented Feb 28, 2011 at 23:08
  • Can you provide an example of how I would access "privateValue"? It doesn't need to be in the constructor. I could a setup or init method that contains the private data I need. Basically I have internal data that a number of methods need to be able to share. Commented Feb 28, 2011 at 23:12
  • sorry for the erroneous answer~ Commented Feb 28, 2011 at 23:17

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.