Question

I want to be able to assign default values to variables when I'm using prototyping for object creation.

When I try to assign default values to the variables they are always 'undefined'.

I have tried to find the answer but all the possible solutions I have tried dont work.

My questions are:

  • why do a variable that have I have initiated with a value has the value 'undefined'
  • how do I solve my problem?

    (function() {
        EmployeeNS = {};
    
        EmployeeNS.Employee =   function() {
                                    var _firstName;
                                    var _lastName;
                                    var _employeeID = 'Unassigned';
                                }
        EmployeeNS.Employee.prototype.setFirstName = function(fName) { this._firstName = fName; };
        EmployeeNS.Employee.prototype.getFirstName = function() { return this._firstName; };
        EmployeeNS.Employee.prototype.setLastName = function(lName) { this._lastName = lName; };
        EmployeeNS.Employee.prototype.getLastName = function() { return this._lastName; };
        EmployeeNS.Employee.prototype.setEmployeeID = function(employeeID) { this._employeeID = employeeID; };
        EmployeeNS.Employee.prototype.getEmployeeID = function() { return this._employeeID; };
    
        EmployeeNS.Worker = function() {
            var _department;
        }
        EmployeeNS.Worker.prototype = new EmployeeNS.Employee();
        EmployeeNS.Worker.prototype.constructor = Worker;
        EmployeeNS.Worker.prototype.setDepartment = function(department) { this._department = department; };
        EmployeeNS.Worker.prototype.getDepartment = function() { return this._department; };
    
    })();
    
    function createWorker() {
        var x = new EmployeeNS.Worker();
        x.setFirstName("John");
        x.setLastName("Doe");
        x.setDepartment("Transport");
    
        var message = x.getFirstName() 
                      + " " 
                      + x.getLastName()  
                      + " (Department: " 
                      + x.getDepartment()
                      + " / EmployeeID: " 
                      + x.getEmployeeID()
                      + ")";
        alert(message);
    }
    

Thanks

No correct solution

OTHER TIPS

you can simply make it to work by changing like this,

  EmployeeNS.Employee =   function() {
                            this._firstName;
                            this._lastName;
                            this._employeeID = 'Unassigned';
   }

Try out this way , you can make those variables truly private by wrapping Employee ,

(function() {
    EmployeeNS = {};

    (function() {
           var _firstName;
                                var _lastName;
                                var _employeeID = 'Unassigned';
    EmployeeNS.Employee =   function() {

                            }
    EmployeeNS.Employee.prototype.setFirstName = function(fName) { _firstName = fName; };
    EmployeeNS.Employee.prototype.getFirstName = function() { return _firstName; };
    EmployeeNS.Employee.prototype.setLastName = function(lName) { _lastName = lName; };
    EmployeeNS.Employee.prototype.getLastName = function() { return _lastName; };
    EmployeeNS.Employee.prototype.setEmployeeID = function(employeeID) { _employeeID = employeeID; };
    EmployeeNS.Employee.prototype.getEmployeeID = function() { return _employeeID; };

    })();

    (function() {
      var _department;        
     EmployeeNS.Worker = function() {

    }
    EmployeeNS.Worker.prototype = new EmployeeNS.Employee();
    EmployeeNS.Worker.prototype.constructor = Worker;
    EmployeeNS.Worker.prototype.setDepartment = function(department) { _department = department; };
    EmployeeNS.Worker.prototype.getDepartment = function() { return _department; };
    })();
})();

Here is the jsfiddle

If you want instance properties, do it like this:

(function() {
    EmployeeNS = {};
    EmployeeNS.Employee = function () {
        this._firstName = null;
        this._lastName = null;
        this._employeeID = 'Unassigned';
    };
    EmployeeNS.Employee.prototype.setFirstName = function(fName) { 
        this._firstName = fName; 
    };
})();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top