2

Can someone tell me why is this printing 'undefined'?

function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
}

// create your Person 
var john = new Person ('John','Doe',33);

// try to print his bankBalance
document.write (john.bankBalance);

In last line I want to print john.bankBalance which is 7500.

3
  • Because john doesn't have a bankBalance property. The variable bankBalance in the constructor function Person is not available outside it (because of JavaScript's scoping rules.) Also, JS has no language support for private properties; though, closures can be used to simulate them. Commented Jul 3, 2016 at 0:59
  • One thing to keep in mind for beginners is that there is no "class" in JS. Instead JS uses prototypes. When you do var bankBalance, it isn't like Java which it declares a member property, it only defines a local variable. To add a property you have to actually change the instance's property (this.bankBalance), or change it via the prototype (Person.prototype.bankBalance = 7500). Commented Jul 3, 2016 at 1:03
  • Thanks to all. Now I know a little bit how Js works Commented Jul 6, 2016 at 0:27

4 Answers 4

2

You need to add this due to the scope of bankBalance only in the constructor. You could do this:

this.bankBalance = 7500;

Alternatively, you could set up a getter/setter for bankBalance like so:

this.getBalance = function() {
    return bankBalance;
}

this.setBalance = function(balance) {
    bankBalance = balance;
}

Here is a JSFiddle demonstrating the functions with getters and setters for the local variable bankBalance.

Here is another JSFiddle with a 'public' bankBalance variable that can be accessed as an instance variable.

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

Comments

1

bankBalance is only available in the function scope. If you want to access publicly, you should define with "this" as you did with firstname and lastname.

Comments

0

This is because of a characteristic of Javascript known as closure. It basically means that if objects are nested, then the inner objects can only access variables from objects outside them. By using the this keyword, your essentially making the variable a property of that object. Variables declared using var will not be accessible to objects that lay outised the method. For an interesting read please check out this link

http://javascript.crockford.com/private.html

Here is an example of an object with a private variable and some public methods and a property. These characteristics are useful as they restrict usage of an object. In this example below we may not want developers tampering with the array so we might just give them a facility to add, facility to remove and they can only see the even indexes : -

function SpecialArray() {

    var _array = [];

    this.length = _array.length;

    this.add = function(stuff) {
        // add some stuff to it
    }

    this.remove = function(stuff) {
        // take some stuff out of it
    }

    this.displayEvenNumbers = function(){
        for (var i=0; i< _array.length ; i+=2){
            console.log(_array[i]);
        }
    }
}

Comments

0

bankBalance as you have written it is scoped to the constructor only. If you want to access it as an instance variable you need to declare it as you have with this.firstname et. al.

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.