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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:39:49+00:00 2026-06-15T07:39:49+00:00

Suppose I have 2 constructors and a member function defined function A() {} function

  • 0

Suppose I have 2 constructors and a member function defined

function A() {}
function B() {}
A.prototype.a = function(){}

And I’ve instantiated a B. How can I force a conversion to make it an A?

o = new B();
//What should I put here?
o.a();

I’m new to js. And I have a feeling that whether o mentioned above is an A or a B is merely controlled by a reference to a prototype. Therefore, I feel typecast should be possible.

Answers, as well as explanations that can help me understand js objects, are welcomed.

  • 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-15T07:39:53+00:00Added an answer on June 15, 2026 at 7:39 am

    You can’t change (that is, completely swap-out) the prototype of an object once it’s been created (yet; in a future version of JavaScript, it’s likely to be possible).

    Objects in JavaScript are enormously malleable, though. You can do this, for instance:

    o.a = A.prototype.a;
    

    …and then o will have the a function. If you wanted to give it all of the enumerable properties of A.prototype (including any from its prototype):

    var name;
    for (name in A.prototype) {
        o[name] = A.prototype[name];
    }
    

    The prototype property of functions is a completely normal, boring object assigned to a normal, boring property on the function. When you use new to create an object using that function (which is called a “constructor function”), the function’s prototype property is assigned to the newly-created object as its underlying prototype (which has no name you can access in code [yet]).

    As of ES5 (reasonably supported in Chrome, Firefox, Opera, and IE9; not in IE8 and earlier), you can get the prototype of an object via Object.getPrototypeOf, and you can create objects assigning them a prototype without going through constructor functions by using Object.create, but you can’t change what an object’s prototype is once it’s been created.

    Some JavaScript engines (like the one in Firefox) have a currently-non-standard extension called __proto__ which lets you directly access and manipulate the prototype of an object, by treating it as a property. E.g.:

    o.__proto__ = A.prototype;
    

    …but that is not standard at present. It may well be part of the next standard, though. If you’re interested in information about the upcoming ES6, you can find draft specifications and such here.

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

Sidebar

Related Questions

Suppose I have two constructor function: var Person = function(xx,xx,xxx,xxxxxxx) { //Person initialization }
suppose i have a .on() function wherein i select multiple ids $(#i, #am, #your,
I have a confusion on class member variable initialization. Suppose in my .h file
Suppose I have class MyObject { Object object1 = new Object(); Object object2; public
Suppose I have a class with 3 constructors, a default (no argument) constructor, a
Suppose you have a class Foo with private member of type Bar. You don't
I have a class which for now should always have a certain member populated
Suppose I have a class that I want to make sure my compiler (GCC
Suppose I have a Generic abstract class that provides some default constructor functionality so
Suppose I have a large list of words. For an example: >>> with open('/usr/share/dict/words')

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.