Solved

Asking for simple code help on Javascript inheritance

Posted on 2014-03-06
3
125 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 52

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39911730
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
ID: 39912841
Thanks!
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39913051
You are welcome - thanks for the points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

910 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

23 Experts available now in Live!

Get 1:1 Help Now