2

Learning JavaScript on codecademy, have several questions about lesson 24/30

In the code below:

  1. why does john.getBalance() (last line) not result in an error when the getBalance function was not declared as this.prototype.getBalance but as this.getBalance? I thought subclasses could only use the functions of superclasses if they were declared with "prototype".
  2. What is the difference between declaring the function this.getBalance vs Person.getBalance?
  3. Is declaring the function inside vs outside the constructor just a convention or does it make a functional difference?
  4. If bankBalance is private, how do I know what functions are allowed to access it? Why is getBalance in the code allowed to access bankBalance?

My code :

function Person(first,last,age) {
    this.firstname = first;
    this.lastname = last;
    this.age = age;
    var bankBalance = 7500;
  
    this.getBalance = function() {
        // your code should return the bankBalance
        return bankBalance;
    };
}

var john = new Person('John','Smith',30);
console.log(john.bankBalance); //undefined

// create a new variable myBalance that calls getBalance()
console.log(myBalance=john.getBalance());
1
  • prototype gives you performance benefits. it enables sharing a method across numerous instances.without prototype each object would have its own method which in turn results in more memory.having a method shared through protoype is not a mandatory requirement. Commented Aug 13, 2014 at 5:57

4 Answers 4

3

1) why does john.getBalance() (last line) not result in an error when the getBalance function was not declared as this.prototype.getBalance but as this.getBalance? I thought subclasses could only use the functions of superclasses if they were declared with "prototype".

As I said in my answer to your last question, there is only one set of properties. Javascript will look first on the object itself and if no property match is found there, it will look on the prototype. There is no separate set of properties that belong to subclass and separate set for superclass. There is one set of properties directly on the object and there is a defined lookup order (for how to search the prototypes) if no matching property is found directly the object itself, but it is logically one overall set of properties on the object (and a method is just a property with a function for its value). There is not one set of properties that belong to the superclass and one set to the subclass. In Javascript you should not think of it that way (I know it feels like that in some other languages, but it isn't that way in javascript).

2) What is the difference between declaring the function this.getBalance vs Person.getBalance?

this.getBalance refers to a method on a specific object.

Person.getBalance refers to a property on the Person constructor which is not something you've declared.

If you mean the difference between declaring this.getBalance and Person.prototype.getBalance, then the main difference is where the function is found in the lookup order. Both will be found on any Person object or any object derived from a Person object. Technically it is more efficient for the runtime if you declare it on the prototype because there is only one shared instance of the function object rather than creating a new instance of the function object for each instance of Person, but operationally they are the same. Using the prototype is preferred unless there is a specific reason not to.

3) Is declaring the function inside vs outside the constructor just a convention or does it make a functional difference?

It is more efficient to declare methods on the prototype, but they can also be declared in the constructor and there is little operational difference except that methods declared in the constructor have access to local variables declared in the constructor which can be used as private instance variables. Using the prototype is preferred unless there is a specific reason not to.

4) If bankBalance is private, how do I know what functions are allowed to access it? Why is getBalance in the code allowed to access bankBalance?

Only functions declared within the constructor (where bankBalance is declared) can access it. This is just plain javascript scoping rules, but comes in handy if you want to implement a private instance variable like this.

Sign up to request clarification or add additional context in comments.

4 Comments

In your answer to my first question, you wrote "...if no property match is found [on the object], it will look on the prototype." Maybe I'm remembering incorrectly, but I remember an instance where a class, say "Animal", had a function, say "Animal.eat()=function()..." and a subclass of animal, say Dog, which didn't have its own eat function, couldn't use the eat() function unless it was changed to Animal.prototype.eat(). So why didn't JS lookup to the Animal superclass to find the eat function?
My second question's wording was a bit unclear. I meant, what is the difference between implementing a hypothetical function as this.functionName=function(){...} vs ClassName.functionName=function(){...}?
@user2752992 - If inheritance is set up properly, there is no case where methods of the superclass cannot be access by anyone. All the methods are on the same object.
@user2752992 - are you asking about the difference between this.functionName = function() {} and Classname.prototype.functionName = function() {}? If so, I added that to my answer to your point 2) above. Classname.functionName is a static function that can only be accessed as Classname.functionName and cannot be accessed on any object created from Classname. It's just a function that is a property of the constructor. It isn't part of any object created by that constructor. You have to use the prototype or assignment to this.functionName for that.
1
  1. this.getBalance exists in every instance, while Person.prototype.getBalance only on the Person class (function). In JavaScript, the inheritance is different from the traditional OOP language.

  2. In your example, you cannot invoke Person.getBalance, no such function is declared.

  3. Different.

  4. In JavaScript, there is no private fields. You only can use closure to simulate this concept.

Comments

1

Let's go over your questions in order.

  1. getBalance() is defined on the Person object as an instance method. That simply means that every Person has its own getBalance() method.

  2. This ties in with the above. If you had declared the method on Person.prototype, it would be shared across all Person objects you create, whereas now each Person has its own getBalance() method. The prototype approach can often provide a performance benefit for this reason.

  3. Not sure what you mean here. Feel free to provide clarification and I'll try to answer.

  4. A function declared inside another function has access to variables declared in the outer function, so getBalance() therefore has access to bankBalance. But since bankBalance isn't a property of the Person object, you don't have access to it via john.bankBalance, which is why that is undefined.

Comments

0
  1. There is no difference

  2. Yes, it will be global or local

  3. There is no allowance

  4. It's all the same

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.