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

  • Home
  • SEARCH
  • 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 8989705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:19:37+00:00 2026-06-15T22:19:37+00:00

I need some help understanding how javascript protoypal inheritance works… or rather why its

  • 0

I need some help understanding how javascript protoypal inheritance works… or rather why its not doing what I am expecting it to do.

Essentially I am trying to establish templated objects that are returned from a new statement. I have a generic constructor function in a utils.js module that assigns any values from a parameter object to a corresponding template object, I return that modified template object as a result of invoking new.


module.exports.ctor = ctor;

//Mapped object constructor
function ctor(template) {

  return function(args) {
     var s, t, p;

    //guard against being used without new
    //courtesy of John Resig http://ejohn.org/blog/simple-class-instantiation/
    if (!(this instanceof arguments.callee)) {
      throw new Error("Constructor called without 'new'");
    }

    //create a deep copy of `template` to modify
    t = JSON.parse(JSON.stringify(template));
    args = args === 'undefined' ? {} : args;   

    // copy values of matching properties from `args` to `t`
    // (uses Crockford's `typeOf` function http://javascript.crockford.com/remedial.html)
    for (p in t) {
      if (args[p]) {
        s = typeOf(t[p]);

        if (s === 'function' || s === 'null') {
          /* do nothing */
        } else if (s === 'array') {
          t[p] = t[p].concat(args[p]);
        } else {
          t[p] = args[p];
        }
      }
    }

    return t;
  };
}

Here is an example of how the generic constructor would work on a Contact template object, with some Contact specific properties added to the prototype object:

var template = {
    email: null,
    phone: null,
    address: []
};

var Contact = require('../util').ctor(template);

Contact.prototype.template = template;

Contact.prototype.print = function() {
  var str = this.email + '\n';
  str += this.phone + '\n';
  for (var i = 0; i < this.address.length; i++) {
    str += this.address[i].toString() + '\n';
  }
};

module.exports = Contact;

My expectation is that the template property and the print function would be available in the returned object’s chain, however it appears that they are not (from node REPL):

> var Contact = require('./mapping/Contact');
undefined
> var c = new Contact();
undefined
> c.print
undefined
> c.template
undefined
> c
{ email: '', phone: '', address: [] }

Can someone explain to me why if I am explicitly adding properties to the prototype of Contact, those properties are not available to the returned object?

  • 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-15T22:19:38+00:00Added an answer on June 15, 2026 at 10:19 pm

    This is because ctor() causes the constructor to return t, which is a plain generic object. Since the constructor is returning the plain object, you lose the prototype. Instead of returning t, copy the properties of t to this:

    for (var k in t) {
        this[k] = t[k];
    }
    

    Then, don’t return anything. Or, return this, if you prefer.

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

Sidebar

Related Questions

I need some help understanding what I'm doing wrong with my js code. Basically
I need some help understanding scope in Javascript. I posted a similar question earlier
I need some help understanding why the ArrayList is not being populated, I am
I am new to ajax and need some help in understanding how javascript runs
I need some help understanding this bit of code pre { white-space: pre; white-space:
OK, I need some help understanding the process behind Facebook's website integration process and
Oracle's PL/SQL is fairly new to me, so I need some help understanding if
I need some help in understanding when and why to use Remote Service instead
I need some help in understanding what is happening here .I am getting a
I have been trying out Cassandra and need some help in understanding a few

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.