[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Asking for simple code help on Javascript inheritance

Posted on 2014-03-06
3
Medium Priority
?
134 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 59

Accepted Solution

by:
Julian Hansen earned 2000 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 59

Expert Comment

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

Featured Post

Industry Leaders: 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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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…
Suggested Courses

649 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