Solved

Asking for simple code help on Javascript inheritance

Posted on 2014-03-06
3
122 Views
Last Modified: 2014-03-07
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.
0
Comment
Question by:elepil
  • 2
3 Comments
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
You are not passing your parameters through to the parent constructor

function Dignitary(firstName, lastName) {
  Person.call(this, firstName, lastName);
};

Open in new window

0
 

Author Closing Comment

by:elepil
Comment Utility
Thanks!
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
You are welcome - thanks for the points.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now