Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7935017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:47:37+00:00 2026-06-03T21:47:37+00:00

Here is my code to achieve my inheritance: <html lang=en> <head> <title>JavaScript Patterns</title> <meta

  • 0

Here is my code to achieve my inheritance:

<html lang="en">
<head>
    <title>JavaScript Patterns</title>
    <meta charset="utf-8">
</head>
<body>
    <script>
        /* Title: Classical Pattern #5 - A Temporary Constructor (a pattern that should be generally avoided)
         Description: first borrow the constructor and then also set the child's prototype to point to a new instance of the constructor
         */


        /* Basic */
        /*function inherit(C, P) {
         var F = function () {};
         F.prototype = P.prototype;
         C.prototype = new F();
         }*/


        /* Storing the Superclass */
        /*function inherit(C, P) {
         var F = function () {};
         F.prototype = P.prototype;
         C.prototype = new F();
         C.uber = P.prototype;
         }*/


        /* Resetting the Constructor Pointer */
        /*function inherit(C, P) {
         var F = function () {};
         F.prototype = P.prototype;
         C.prototype = new F();
         C.uber = P.prototype;
         C.prototype.constructor = C;
         }*/


        /* in closure */
        var inherit = (function () {
            var F = function () {
            };
            return function (C, P) {
                F.prototype = P.prototype;
                C.prototype = new F();
                C.uber = P.prototype;
                C.prototype.constructor = C;
            }
        }());


        function Parent(name) {
            this.nameParent = name || 'Adam';
            this.parentName = "parent";//this.nameParent;
        }


        // adding functionality to the prototype
        Parent.prototype.say = function () {
            return this.nameParent;
        };



        // child constructor
        function Child(nameChild) {
            console.log("parentprop:" + this.parentName);
        }

        inherit(Child, Parent);


        var kid = new Child();
        console.log(kid.name); // undefined
        console.log(typeof kid.say); // function
        kid.nameParent = 'Patrick';
        console.log(kid.parentName);
        console.log(kid.say()); // Patrick
        console.log(kid.constructor.nameParent); // Child
        console.log(kid.constructor === Parent); // false




        // reference
        // http://shop.oreilly.com/product/9780596806767.do
    </script>
</body>

I need the Child console.log to display “parent” from the parent class inherited, but for now, it only displays undefined.

I don’t know why it doesn’t inherit from parent property.

Thanks in advance for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:47:40+00:00Added an answer on June 3, 2026 at 9:47 pm

    Because you never call the function Parent, which is what’s setting the property. If you look closely at your inherit function, you’re using the prototype property of P but you’re never calling P — which is a good thing; it’s Child who should call P:

    function Child(nameChild) {
        Parent.call(this);
        console.log("parentprop:" + this.parentName);
    }
    

    Obviously, this has the disadvantage that you have to list Parent in more than one place — both in your call to inherit, and in Child. You could mitigate that by setting the parent constructor on the child function in inherit:

    var inherit = (function () {
        var F = function () {
        };
        return function (C, P) {
            F.prototype = P.prototype;
            C.parent = P;          // <==== The new bit
            C.prototype = new F();
            C.uber = P.prototype;
            C.prototype.constructor = C;
        }
    }());
    

    …so then Child becomes:

    function Child(nameChild) {
        Child.parent.call(this);
        console.log("parentprop:" + this.parentName);
    }
    

    If you’re interested in doing hierarchies thoroughly (with support for calling “super” methods and such), you might want to look at my Lineage toolkit. It’s a small toolkit that automates a lot of this stuff, but if you want to learni how this stuff works, the source might make for interesting reading. There’s also a wiki page there showing multi-layer, fully-functional inheritance without using Lineage (as a means of comparing it with using Lineage).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a such html somehtml = <p>Here is my solution: </p><pre><code> some code here
CODE HERE: http://jsfiddle.net/B7Y43/ Hello fello programmers, I have the following situation: My PHP-script generates
I'm currently working on some code based on John Resig - Simple JavaScript Inheritance
Ok the error is showing up somewhere in this here code if($error==false) { $query
Here is code from MSDN . I don't understand why the work isn't just
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
here is code for regular expression matching #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int
Here is code example class A{ int i; public: A(int i) : i(i) {}
enter code here I have a table on SQL server 2005 with bigint primary
The code here is X++. I know very little about it, though I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.