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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:56:03+00:00 2026-06-07T17:56:03+00:00

I was playing around with JavaScript in particular simulating object oriented programming with classes

  • 0

I was playing around with JavaScript in particular simulating object oriented programming with classes and whatnot.

I knew about this way of achieving inheritance

MyClass.prototype = new AnotherClass();

But I wasn’t satisfied, I didn’t like how I needed to call the constructor of AnotherClass. So I was playing around and I came up with something that seemed to work and basically want a second opinion.

function clone (obj)
{
    function CloneFactory () {}
    CloneFactory.prototype = obj;

    return new CloneFactory();
}

MyClass.prototype = clone(AnotherClass.prototype);

By cloning the prototype we get a new copy of it and assign that to MyClass‘s prototype so that changing the inherited properties will not affect the parent’s prototype’s properties. Like this would MyClass.prototype = AnotherClass.prototype.

I ran stress tests and this is more efficient under certain circumstances, i.e. when there’s a lot of code in the parent’s constructor, otherwise it’s about the same. Another benefit (or at least I find it to be beneficial) is that it allows to some extent information hiding from the subclasses. Any privileged methods and members will NOT be inherited.

Is there some major pitfall that I’m overlooking?

I’m not an expert with JavaScript, actually I’m fairly new to JavaScript, so I’d like to have a second opinion on this because I can’t seem to find anything through Google. I don’t want to implement bad code :)!

  • 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-07T17:56:04+00:00Added an answer on June 7, 2026 at 5:56 pm

    This is almost exactly what Object.create does. The function you’ve written is a pretty standard “polyfill” for that method.

    This is a really common way of abstracting object creation in a way that more closely reflects “true” prototypal inheritance. Definitely a safe way to do things.

    Oh, and here’s a link to the MDN entry for Object.create, if you’re interested: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create/

    You’ll notice at the bottom that they actually include the polyfill, which is pretty much identical to your code, save for some safety checks and variable names.

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

Sidebar

Related Questions

I was playing around with some Javascript snippets today and noticed that this code
I was playing around with javascript prototype chain inheritance and i came accross this
I was playing around with arrays in Javascript today and noticed this little gem:
So I'm playing around with JavaScript and came across what I think to be
I've been playing around with JavaScript extending its functionality but have noticed some behaviour
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
Been playing around with this for a couple of hours and can't seem to
I was playing around with some JavaScript code and found that when I took
Possible Duplicate: Is JavaScript's Math broken? I have just started playing around with Javascript
I currently playing around with the Facebook JavaScript SDK and the Scores API (

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.