reinitializing the prototype object's constructor property

Let's analyze the code snippet provided:

function shape(){
    this.name = "2d shape"
}

function triangle(){
    this.name = "triangle";
    this.ttest = function(){
        alert("in triangle constructor");
    }
}

function equitriangle(){
    this.name = "equitriangle"
}

var s = new shape();
triangle.prototype = s;
equitriangle.prototype = new triangle();

var et = new equitriangle();

alert(et.name); // This will display 'equitriangle'
et.ttest(); // This will display 'in triangle constructor'
alert(x.isPrototypeOf(et));// This will display true
alert(et.constructor); // **This will mistakenly display the shape constructor instead of the equitriangle constructor** 

Question 1) Why is the shape constructor being displayed?

However, if we add the following line before creating an instance of equitriangle:

equitriangle.prototype.constructor = equitriangle;
var et = new equitriangle();
....
.....
.....
alert(et.constructor); // Now it correctly displays the equitriangle constructor as expected.

It is common knowledge that overwriting a prototype object can lead to unexpected outcomes. Hence, it is advised to "reset" the constructor which brings us to the solution proposed.

equitriangle.prototype.constructor = equitriangle;

Question 2) How does the above line provide a resolution?

Answer №1

What is the reason for receiving the shape constructor?

Keep in mind that et.constructor is a property passed down by the prototype:

et.constructor; // shape
et.hasOwnProperty('constructor'); // false

Therefore, .constructor is inherited from equitriangle.prototype.

It should also be noted that

equitriangle.prototype.constructor
is inherited from triangle.prototype.

Furthermore, triangle.prototype.constructor is inherited from shape.prototype.

In conclusion,

shape.prototype.hasOwnProperty('constructor')
evaluates to true

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

Ajax causing unreliable posts

I am facing an issue with sending and receiving data in my mobile application. I currently use the jquery $.post function, but it seems to be quite unreliable. Issue: Occasionally, about 1 out of 10 times, the POST request is sent successfully, but the ca ...

Is the custom attribute event being triggered too soon?

My Unique Component Creation Journey I have meticulously crafted a custom component to enhance the navigation of my application. The core structure consists of an ul element, with each li item dynamically generated based on the contents of the router&apo ...

Steps for adding a React Class Component into a Modal that is not within the React Tree

Our project is built using PHP MVC Framework and we initially used jQuery as our main JavaScript framework for handling UI activities. However, we have now transitioned to using React.js. My query is about how to inject or append a React Functional/Class-b ...

Utilize anychart.js to define the axis using JSON data

I'm relatively new to using anychart js and encountering some obstacles. I have a json file that is being fetched from an API, containing data on NBA players' statistics. You can find the json file here: My goal is to display the date data on th ...

There is a necessary pause needed between carrying out two statements

I am currently working with extjs 4.2 and I have encountered a situation where I am loading the store object in the following manner: var userDetailStore = Ext.create('Ext.data.Store', { model: 'Person.DetailsModel', autoLoad: ...

ng-if behaving unexpectedly

This is a demonstration of how I populate the Table and attach checkboxes to a controller: <tr ng-repeat="key in queryResults.userPropNames"> <td><input type="checkbox" data-ng-checked="selectedKeys.indexOf(key) != -1 ...

Troubleshooting: jQuery animation for background-color doesn't function in Internet Explorer

I've been working on a website and I'm trying to create a navigation bar similar to the one found at . My goal is to have the navbar transition from transparent to a colored background as the user scrolls down the page. For this project, I am ut ...

What is the best way to handle multiple promises when loading a state in Angular?

When loading the /home state, I need to retrieve all users from the database in order to customize the home controller and view based on the logged-in user. Currently, in the :resolve section of the state configuration, I am fetching all 'posts' ...

The pageSize in React's Material Table does not reflect dynamic updates

Currently, I am attempting to implement pagination for material table data using TablePagination. One issue I am facing is that the pageSize property, initially defined as a state variable, does not update when the selected pageSizeOptions change. Despite ...

GraphQL is not capable of fetching data directly from a mysql database

Trying to incorporate GraphQL with MySQL in a Node.js Express server has been my recent challenge. Unfortunately, every time I execute my query, an error surfaces. Here is the specific error message: { "errors": [ { "message&quo ...

Passing the onChange event in React to a nested child component

The code snippet below illustrates a scenario where the user encounters an error: import React from 'react'; import {render} from 'react-dom'; import Form from './form.jsx'; import axios from 'axios'; class App e ...

Store data in LocalStorage according to the selected value in the dropdown menu

Can you help me understand how to update the value of a localstorage item based on the selection made in a dropdown menu? <select id="theme" onchange=""> <option value="simple">Simple</option> <option valu ...

Ways to prevent mouse selection in an input field without disabling or making it read-only

Imagine this scenario: <input type="text"> The task at hand is to prevent text selection within the <input> field. ...

Learn how to utilize the import functionality in Node.js by importing functions from one .js module to another .js module. This process can be seamlessly integrated

Hey there! I'm currently facing a challenge where I need to import a function from one JavaScript file to another. Both files are on the client side in Node.js, so I can't use the require method. If I try to use the import statement, I would need ...

Flask is failing to display AJAX data

Looking to embark on a Flask project involving sending an AJAX request. In the Flask code, I want to assign a variable to handle the request and display it on the page using a Jinja variable. Flask from flask import Flask,render_template,request app = Fla ...

Discovering the smallest and largest values in an object literal using JavaScript

I am looking to extract the minimum value from an object literal and utilize it in AngularJS, as shown below: scope.data = { 'studentName' : "Anand", 'socialStudies' : "98", 'english' : "90", 'math' ...

What is the process for deselecting text from a text field?

After performing a search and displaying the results on my search form, I've noticed that the text query in the textfield is being marked as selected text even though I don't want it to be. How can I prevent this? Fiddle 1) What is the best app ...

What steps should I take to ensure my API secret remains secure during my next.js deployment?

Recently, I created a small internal application using React and Next.js. To keep my API key and secret secure, I followed the instructions on how to use an .env file provided by Next.js. In my api/hello.js file, I have a simple request that looks like th ...

Use Javascript to deactivate the mouse cursor and rely solely on the keyboard cursor for navigation

I am facing an issue with a div that contains a textarea. The cursor is automatically positioned at the beginning of the text within the textarea. I would like to disable the mouse cursor when hovering over the textarea but still be able to navigate within ...

Encountering an issue in next js where attempting to access properties of an undefined variable is resulting in an error with the

"next": "13.0.7" pages version An error occurred in production mode, displaying the following message in the console: TypeError: Cannot read properties of undefined (reading 'push') Additionally, an application error popped ...