Using Object.create to establish multiple prototypes in JavaScript

I am trying to have my child object inherit the prototypes of multiple parents, but the following methods do not seem to work:

child.prototype = Object.create(parent1.prototype, parent2.prototype);

and also this:

child.prototype = Object.create(parent1.prototype);
child.prototype.add(Object.create(parent2.prototype));

Do you have any suggestions on how to achieve this?

UPDATE : I am working with THREE.JS on Chrome

Answer №1

In Javascript, achieving multiple inheritance is not directly supported. The common approach is to extend a base class. The following code snippet demonstrates this concept, inspired by the example found on MDN Object.create

function SuperClass(){
  //class 1 - contains some super functionalities
  console.log('SuperClass');
}
function OtherSuperClass(){
  //class 2 - contains some other super functionalities
  console.log('OtherSuperClass');
}

//MyClass intends to inherit the functionalities from both SuperClass and OtherSuperClass
function MyClass() {
  SuperClass.call(this);
  OtherSuperClass.call(this);
}

//implementing inheritance and extension
//using Object.assign to extend the class
MyClass.prototype = Object.create(SuperClass.prototype); // inheritance
Object.assign(MyClass.prototype, OtherSuperClass.prototype); // extension

//defining custom method or overriding existing ones
MyClass.prototype.myMethod = function() {
  console.log('custom method for double inheritance');
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to Spidermonkey Bytecode Documentation

I've been searching for a comprehensive guide to spidermonkey's bytecodes for some time now, or at least something that gives me an overview of their purpose. Does anyone know of a good resource for this? Thank you! ...

Obtaining the data stored in objects within a parse database

I'm currently facing an issue where I am trying to retrieve the name of the creator from the session object, which is a pointer. For testing purposes, I have been attempting to access this information but it keeps showing up as undefined. Any suggesti ...

One requirement for a directive template is that it must contain only a single root element, especially when using the restrict option to E

I am currently managing an older AngularJS application (v1.3.8). Why is the demo application showing me this error? The directive 'handleTable' template must have only one root element. sandbox.html <!DOCTYPE html> <html> <he ...

Saving a picture to local storage with the file input type in ReactJS

I am attempting to save an image in the browser storage once a user selects an image from their computer. <div className="add_grp_image_div margin_bottom"> <img src={img_upload} className="add_grp_image"/> <input type="file" class ...

Error message related to the callback type issue in Node.js with the http.createServer

Having recently embarked on my journey with node.js, I delved into various tutorials and encountered a stumbling block while attempting to refactor some code. The tutorial that caught my attention and led me to this hiccup can be found here: http://www.tu ...

Cypress eliminating the "X-CSRFToken" header

There seems to be an issue with the Cypress test runner where it is removing X-CSRFToken from the request header, leading to a 403 Forbidden error. I have compared the headers between a manual run and a Cypress test run, and you can see the difference in t ...

Having trouble with Electron nodeIntegration functionality and experiencing some odd behavior in general with Electron

Having just started working with Electron, I find myself encountering some puzzling behavior that has me stumped. Here's a quick summary of the issue: I am unable to establish communication between Electron and the HTML "Uncaught ReferenceError ...

Drag and Drop Functionality in ReactJS for Organizing Multiple Lists

After hours of searching, I have yet to find a React library that can handle sorting between multiple lists. The closest solution I came across was in this article: There is also an example provided here: The issue with this solution is that you have to ...

What steps can be taken to resolve the mouse-pointer problem?

Currently, I am utilizing PHP, jQuery, JavaScript, and other tools for my website development. I have a new requirement for a script in jQuery/JavaScript that can trigger an alert when the user's mouse-pointer moves away from the tab where my website ...

phpif (the current date is after a certain specific date, do

Can someone please help me solve this problem? I want to prevent echoing a variable if the date has already expired. Currently, my PHP code displays: Match 1 - April 1, 2015 Match 2 - April 8, 2015 What I need is for Match 1 to not be echoed if the cur ...

JavaScript is utilized to update HTML content through an Ajax request, but unfortunately, the styling is

Apologies for the unconventional title, I am currently in the process of creating a website. When the page is initially loaded, I call upon two scripts within the 'head' tag to create an attractive dynamic button effect. <script src="https:// ...

Navigable Vuetify Tabs with routing capabilities

Within my Vue application, I have a page that contains various tabs. My goal is to display different tabs based on the routes being accessed. To achieve this functionality, I followed an answer provided in this link. Overall, it's working well! I ca ...

Combining both the Javascript and PHP components of the Facebook SDK

I recently integrated the JavaScript version of Facebook SDK, but now I'm unsure how to also integrate the PHP version without causing any conflicts. Here are my questions: If I implement the PHP SDK from Facebook, is there a way to detect if the J ...

Navigating through Next.js for slug URLs such as site.com/username

Can someone help me figure out how to create a profile page for each username, like site.com/jack or site.com/jill? I'll be pulling the username info from an API that also contains the user ID and email. I'm new to Next.js and would really appre ...

What is the best method for dividing strings in javascript?

After invoking a JavaScript function, I received the following results: (1, 00), (2, 10), (3, 01), (4, 11) I am looking to store this data in an array or JSON format like this: [{id:1, number: 00},{id:2, number: 10},{id:3, number: 01},{id:4, number: 11} ...

Use Google script to input drop down options directly into a Google spreadsheet

After gathering coding advice from various StackOverflow examples, I've hit a roadblock while attempting to take my script to the next level. The task seems simple enough, but for some reason, I just can't figure it out. Here's the situati ...

Ways to initiate SVG animations using Angular Component functions?

I am currently working on a project where I want to incorporate an animation that reflects the sorting process of an array of numbers. However, despite successfully sorting the numbers in the array, I am facing challenges with triggering the animations. I ...

Issue: Access to sdcard/hello.mp3 file denied due to permission error (EACCES)

While testing the "react-native-audio-recorder-player": "^3.5.3" on Android 11 & 12 with targetSDKversion 31, I encountered the error message Error: sdcard/sound.mp3: open failed: EACCES (Permission denied). Numerous suggested solut ...

Tips for customizing the appearance of a single plot within a time series chart using FusionCharts or FusionTime

I've implemented a time series line graph using FusionCharts in the following code snippet: const MyChart = () => { const schema = [ { name: "Category", type: "string" }, { ...

What causes a decrease in performance for a React + Three/Threejs application when it is deployed or in production as opposed to running on localhost?

Unfortunately, I do not have a solution for this problem. I am currently facing an issue with my React application using Three.js where the zoom feature performs much better in my local development environment than on the deployed site. The application is ...