Solved

Asking for simple code help on Javascript inheritance

Posted on 2014-03-06
3
130 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 57

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 57

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
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 …

739 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