Are prototype properties of constructor functions enumerable?

Despite going through various posts and documentation, I am still puzzled about the exact definition of an enumerable property. Let me illustrate my confusion:

I have designed a constructor function and added a prototype to it.

var myAlphabet = function() {
  this.c = 5
  this.d = 6
}

myAlphabet.prototype = {
  e:7
}

Next, I create a new instance of myAlphabet using the new keyword

var myObject = new myAlphabet();

Using a for-in loop, my intention is to display all the keys in the instance of myObject (excluding keys from the prototype).

for( key in myObj){
    console.log(key);
}

This results in the following output:

'c'
'd'
'e'

Based on the information provided in the for..in loop documentation:

The for..in statement iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.

This leads me to believe that the prototype is considered an enumerable property. However, while reading about Enumerable properties, I came across the following explanation:

Ownership of properties is determined by whether the property belongs to the object directly and not to its prototype chain.

Since the prototype created earlier does not directly belong to the instance of myObject but is part of the prototype chain, I wonder why it gets included when looping over each key?

Answer â„–1

Why does this happen when I iterate through each key?

This is due to the way JavaScript's objects inherit values from their prototype.

Just like in classes, where instances inherit properties from their parent class:

class:base
{
    c:2
    d:3
}
base
{
    a:1
}

If you create an object of type myAlphabet, it will have properties a, b, and c. The difference is that in class-based languages, the instance would "contain" all the values—those defined by itself and those inherited from the parent class:

instance of class
{
    a:1
    c:2
    d:3
}

In prototypal languages, objects derive from other objects, meaning the values do not reside in the instance itself but in the object that acts as its parent:

object1
{
    prototype:object2
    c:2
    d:3
}
object2
{
    prototype:null
    a:1
}

This allows for virtual inheritance similar to class-based languages, with the main difference being that the inheritance happens dynamically. If you change object2.a = new, then reading object1.a will fetch the updated value new from the parent.

To check if a property belongs to the object itself or is inherited from the parent, use object1.hasOwnProperty(a).

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

What is the most effective way to assign req.body values to an attribute of a class?

I am currently working on a class that serves as a controller to execute a method when called from a route. In the list method, I am trying to assign a value to my dataStore attribute without specifying a particular type since I am unsure of what type it s ...

Unable to relocate the cursor to an empty paragraph tag

Wow, I can't believe how challenging this issue is. My current project involves implementing the functionality for an enter key in a content editable div. Whenever the user hits enter, I either create a new p tag and add it to the document or split t ...

Supplier for a module relying on data received from the server

My current component relies on "MAT_DATE_FORMATS", but I am encountering an issue where the "useValue" needs to be retrieved from the server. Is there a way to make the provider asynchronous in this case? export const MY_FORMATS = { parse: { d ...

Is your Node.js EventLoop operation stuck in limbo?

This pertains to a Node/Express/MongoDB application, involving an AJAX call Previously, the selector.html() event was updating the html content of the selector, but now it is not. The only modification made was the addition of an editAll() function in ...

What is the best way to transform a Ruby hash into a JavaScript object while maintaining unquoted values?

Is there a way to display a Ruby hash as a JS object without quotes for certain values in the hash, essentially as JS code? For instance, consider the following Ruby code: { foo: proc { 'someJavascriptFn()' } }.to_json How can I have the JS out ...

What exactly is data binding in Google Sheets all about?

Is there a way to use Google Apps Script to connect two cells together? For instance, if one cell in a sheet is modified, can it automatically update a corresponding cell in another sheet? For example, let's say in sheet1 there is a "money spent" val ...

When attempting to import my JSX file into page.js, I continue to encounter the error "module not found." How can I troubleshoot and resolve this issue in Visual Studio Code

I recently created a new file called mysec.jsx in the components folder of src. I then used the export function to properly export it. However, when I tried to import this file in page.js using the import function, I encountered an error message that said: ...

What should I do to resolve the error message TypeError: _components_firebase_Firebase__WEBPACK_IMPORTED_MODULE_2__.default.auth is not a valid function?

I have implemented Firebase with next.js and organized my files as shown below. However, I am encountering an issue with using the firebase client side SDK during the sign-up process. Firebase.js is where the firebase app is initialized import firebase fr ...

JavaScript popup cannot be shown at this time

I'm encountering an issue with displaying popups using JavaScript. Currently, only the div with class "popup" is being shown. When a user takes action, both popup and popup2 should be displayed but for some reason, it's not working as expected. ...

Navigating through various features in nodejs / expressjs

Managing multiple functions in nodejs/expressjs can be a bit confusing for those used to PHP. In PHP, you simply call one function after another, but in node, things work differently with callbacks and potential errors related to undefined variables. Her ...

What advantages does JWT have over Firebase that make it the preferred choice?

In our authentication systems, we have the option to verify a user through Firebase or by their stored email in the database. Despite having these methods of verification, why do we incorporate JWT (json web token) into our processes? What distinct advan ...

Automatically Switch Font Colors to Red and Green

I am looking to automate the process of changing multiple textbox colors based on the class property. <input type="text" Class="ChangeColor" /> <input type="text" Class="ChangeColor" /> <input type=& ...

Unable to execute the 'getElementsByTagName' function as null value is passed as the parameter

I'm encountering a problem with my first AJAX attempt and my first time using a .php file. I'm working through an exercise in the text, but things aren't going as expected. To troubleshoot, I've been using the alert function extensively ...

Can we load the form using an ajax request and then submit the form using ajax as

So I have a situation where I am loading a form into a page using an ajax call as shown below: $('.ajax_click').on('click', function(event) { event.preventDefault(); /* Act on the event */ var getmsgtoload = $(this).find(&ap ...

Ensure that each function is completed before proceeding to the next one

I've encountered an issue with my react app. After a user submits a form, I need a modal to open and the user's response to be stored in state. Then, based on this response, I have to execute some business logic before finally submitting the form ...

What steps can you take to stop a tab from being inserted if one is already present?

I am facing a simple issue where I need to prevent the insertion of a tab if one already exists. Issue: I have a search bar that displays results in a div with class.result_container_2 when a user inputs a name. Upon clicking on this tab, another tab is i ...

Finding image input loading

When working with the following code: <input type="image" ... onLoad="this.style.opacity = 1" /> Everything seemed to be functioning well in IE, but encountered an issue in Chrome where the onLoad event failed to trigger upon image load. It's ...

Sideways scrolling carousel/slider

Currently, I am in the midst of a project page where my aim is to incorporate a horizontal scrolling project/image slider. Strangely enough, all my attempts thus far have failed to produce the desired outcome. This is the layout: the projects must transit ...

After the rendering process, the React Component member goes back to a state of

One issue I encountered is related to a component that utilizes a separate client for making HTTP requests. Specifically, when trying to use the client within a click event handler, the call to this.client.getChannel() fails due to this.client being undefi ...

Error in Node.js: Trying to access the 'body' property of an undefined variable

While working on a project for a Udacity course, I encountered a stumbling block. The issue revolves around capturing user input from a form and making a post request to return a javascript object. However, when attempting to run the server with node js, I ...