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 7766005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:18:22+00:00 2026-06-01T15:18:22+00:00

I have this JavaScript: var Type = function(name) { this.name = name; }; var

  • 0

I have this JavaScript:

var Type = function(name) {
    this.name = name;
};

var t = new Type();

Now I want to add this:

var wrap = function(cls) {
    // ... wrap constructor of Type ...
    this.extraField = 1;
};

So I can do:

wrap(Type);
var t = new Type();

assertEquals(1, t.extraField);

[EDIT] I’d like an instance property, not a class (static/shared) property.

The code executed in the wrapper function should work as if I had pasted it into the real constructor.

The type of Type should not change.

  • 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-01T15:18:24+00:00Added an answer on June 1, 2026 at 3:18 pm

    update: An updated version here

    what you were actually looking for was extending Type into another Class. There are a lot of ways to do that in JavaScript. I’m not really a fan of the new and the prototype methods of building “classes” (I prefer the parasitic inheritance style better), but here’s what I got:

    //your original class
    var Type = function(name) {
        this.name = name;
    };
    
    //our extend function
    var extend = function(cls) {
    
        //which returns a constructor
        function foo() {
    
            //that calls the parent constructor with itself as scope
            cls.apply(this, arguments)
    
            //the additional field
            this.extraField = 1;
        }
    
        //make the prototype an instance of the old class
        foo.prototype = Object.create(cls.prototype);
    
        return foo;
    };
    
    //so lets extend Type into newType
    var newType = extend(Type);
    
    //create an instance of newType and old Type
    var t = new Type('bar');
    var n = new newType('foo');
    
    
    console.log(t);
    console.log(t instanceof Type);
    console.log(n);
    console.log(n instanceof newType);
    console.log(n instanceof Type);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: <script type=text/javascript> var maxLength=10; function charLimit(el) { if (el.value.length >
Okay, I have this code: <script language=javascript type=text/javascript> <!-- function requestPage(page) { var request
I have used Javascript onlaod like this: function check() { var pic = new
I have this javascript code <Script> function getGroupId(){ var group=document.getElementById(selectedOptions).value; var groupFirstLetter=group.substring(2); } </Script>
i have this javascript code to which i want to add the jquery fade-in
I have this JavaScript code: for (var idx in data) { var row =
I have this piece of code in javascript: var catId = $.getURLParam(category); In my
I have a javascript that does this (http is your XMLHttpRequest object) var r
I have this Javascript data: [{id:123,type:test},{id:154,type:another}] How would you transform that into something so
I have the following Javascript code: function checkIfValid(){ var form = document.createuserform; if(form.fName.value ==

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.