Avatar of elepil
elepil

asked on 

Asking for simple code help on Javascript inheritance

I created a Person object, and I'm trying to create another object called Dignitary that inherits from the Person object. Somehow, it's not working. Can someone please tell me what I'm doing wrong?

    function Person(firstName, lastName) {
        var _firstName = firstName;
        var _lastName = lastName;
        
        Person.prototype.__defineGetter__("firstName", function() {
           return _firstName; 
        });
        
        Person.prototype.__defineSetter__("firstName", function(value) {
            _firstName = value;
        });
        
        Person.prototype.__defineGetter__("lastName", function() {
            return _lastName;
        });
        
        Person.prototype.__defineSetter__("lastName", function(value) {
            _lastName = value;
        });
    };
    
    var p1 = new Person("John", "Doe");
    
    console.log(p1.firstName); // Prints John
    console.log(p1.lastName); // Prints Doe
    
    function Dignitary() {
        
    };
    
    Dignitary.prototype = new Person(); // making Dignitary inherit Person
    Dignitary.constructor = Dignitary; 
    
    var d1 = new Dignitary("Jane", "Bimbo");
    console.log(d1.firstName); // THIS IS COMING OUT UNDEFINED, WHAT AM I DOING WRONG?

Open in new window


Thanks in advance.
Scripting LanguagesJavaScript

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon