使用原型设计为javascript分配默认值

时间:2014-01-22 10:23:15

标签: javascript variables object prototyping

我希望能够在使用原型创建对象时为变量指定默认值。

当我尝试为变量指定默认值时,它们总是“未定义”。

我试图找到答案,但我尝试过的所有可能的解决办法都没有用。

我的问题是:

  • 为什么我使用值启动的变量具有值' undefined'
  • 如何解决我的问题?

    (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);
    }
    

由于

2 个答案:

答案 0 :(得分:0)

你可以通过改变这样来简单地使它工作,

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

尝试这种方式,您可以通过包装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; };
    })();
})();

以下是jsfiddle

答案 1 :(得分:0)

如果您想要实例属性,请执行以下操作:

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