Distinctive titles for JavaScript constructors/prototypes compared to classes

When working with JavaScript ES6, classes allow us to write code like this:

class RectangularShape {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }

  getArea() {
    return this.height * this.width
  }

  static someStaticMethod() {
    // performs some utility function
  }
}

This syntax is essentially just a cleaner version of the older ES5 code shown below:

function RectangularShape() {
  this.height = height;
  this.width = width;
}

RectangularShape.prototype.getArea = function() {
  return this.height * this.width
}

RectangularShape.someStaticMethod = function() {
  // performs some utility function
}

In an ES6 class, we can identify the following roles:

  • RectangularShape is a class
  • getArea is an instance method
  • someStaticMethod is either a class method or a static method

While teaching about objects, prototypes and classes, it's important to use the correct terminology. However...

What are these components classified as in the context of ES5 JavaScript? Here's my breakdown:

  • RectangularShape is a constructor function
  • getArea is a prototype method
  • someStaticMethod is a constructor method

I'm unsure if the terminology should be consistent between ES5 and ES6, or if the terms I've used are completely accurate.

Answer №1

Rectangle is a function used for creating new objects

Absolutely, that description fits because it is indeed a function that helps in constructing new instances.

getArea is a method associated with the prototype

In this context, a method refers to a function as a property, and yes, it is part of the prototype chain.

someStaticMethod is a method linked to the constructor

This might be a bit misleading since it suggests something being constructed, but it's more like a method attached to the constructor. Personally, I often refer to it as a "static method."

Answer №2

Your description of the names provided is mostly accurate, although there may be some room for debate when it comes to the last one:

Rectangle.someStaticMethod = function() {
  // performs a utility function or similar task
}

In your statement:

someStaticMethod is considered a constructor method

You could classify it as such, or simply as a method, but in essence, all of the terms mentioned in your query are valid.

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

Which medium is the optimal choice for file manipulation in Node.js and JavaScript?

Is there a simple way for JavaScript to receive incoming data from Node.js that was obtained from an external server? I've been searching for a solution to this problem for days now. Here's my current approach: I plan to have both the server-sid ...

The Next.js page suddenly disappears after a moment

Out of the blue, my next.js page suddenly went blank for no apparent reason. I didn't make any changes, it just happened. I attempted to restart my dev server and even deleted the '.next' folder, but nothing seemed to fix the issue. Despite ...

Implementing relationships in microservices with Prisma and NestJS

Schema for User Management service: model User { id String @id @default(uuid()) username String @unique email String @unique password String } Schema for File Management service: model File ...

Output the JSON string retrieved from a specified URL

I'm currently working on using ajax to retrieve and parse JSON data from a specific URL. I am looking for assistance on how to store the parsed array into a variable. Any guidance or suggestions would be greatly appreciated. Thank you! function rvOff ...

Continuously receiving the "Add to home screen" prompt despite already installing the PWA app

Is there a method to determine if the Progressive Web App has already been installed? It is possible to cancel the prompt event in the 'beforeinstallprompt' event. window.addEventListener('beforeinstallprompt', (event) => { // co ...

"Adjusting the height of Material UI card content to ensure fixed positioning at the bottom

Currently, I am working on mapping material-ui cards with dynamic content from a JSON object, resulting in varying card heights. I have successfully set a fixed height for each card, but the content is not aligned correctly. You can see the issue in this ...

Troubleshooting TypeScript Modules in Visual Studio 2015 Update 2: Fixing the 'require' Undefined Error

While working in Visual Studio 2015 Enterprise with Update 2 installed, I decided to create a new TypeScript project called TypeScriptHTMLApp1 using the default template and settings. As part of this project, I added a new TypeScript file named log.ts and ...

The concept of an undefined property

I encountered this error in my console: index.js:70 Uncaught TypeError: Cannot read property 'searchForm' of undefined at eval (index.js:70) at Module../src/js/index.js (bundle.js:4245) at __webpack_require__ (bundle.js:20) at e ...

Error: the keyword "module" has not been properly defined

Struggling to run this web app, initially encountered: (node:12960) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. C:\Users\J\react-messenger\stream-chat-boilerp ...

IE8 does not show AJAX response right away

I have encountered a peculiar problem with the code below. In Internet Explorer 8, the content retrieved through xmlHttpRequest does not appear immediately after clicking the link. It only displays after moving the mouse or cursor following the click. < ...

Extract the array of numbers from the JSON file

My task is to extract an array of numbers from JSON files. I need to handle files that contain only arrays of numbers or one level deeper (referred to as direct arrays). These files may also include other data types such as strings or booleans. The challe ...

Unique title: "Personalized on-click.prevent feature"

I'm having trouble coming up with a name for this concept, so I don't know what specific term to search for. I've checked out some directives, but I'm not convinced that's what I need. Essentially, I want to be able to do the follo ...

Utilizing Radio buttons for validation in react-hook-form

After creating a form with radio buttons and implementing validation using React Hook Form, I encountered an issue where the console always displayed "on" regardless of the selected radio button. <div className=""> <label ...

After submitting the form, I must only deactivate the checkbox that has been selected

Here is the HTML code I am working with: I have multiple checkboxes designed as buttons to select a room, but it's quite annoying when they all get selected at once. $("form").submit(function() { $('.seat input:checkbox').prop("disable ...

Invalid extra parameters in the $.ajax function

I am attempting to access a web service in order to retrieve some data. I must include this URL using the GET method: http://localhost/ecosat/ws/api.php?t=vw_motorista However, when I check in Chrome Developer Tools, the link is shown as: http://localho ...

Looking to display all items once the page has finished loading

I am experiencing a minor issue. Every time I access my store page where all products are listed, I have to click on the size filter to load the products. This is not ideal as I want all products to be displayed automatically when the page loads. What modi ...

I have developed a function that adds up price values, but for some reason it is always lagging one step behind

I am facing an issue with my React container that has add and subtract functions. These functions are triggered whenever a user clicks on '+' or '-' spans, to calculate the total 'price' of three different 'products' ...

Issue with JavaScript HTML Section Changer not functioning properly

I need to implement a button that switches between two pages when pressed. Although the code seems simple, I am struggling to make it work. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"& ...

Tips for showcasing a dataset within a table using Angular.js ng-repeat

I am encountering an issue where I have to present an array of data within a table using Angular.js. Below is an explanation of my code. Table: <table class="table table-bordered table-striped table-hover" id="dataTable" > <tbody> ...

Guidelines for combining inner objects with their parent in Javascript

I need assistance with using the filter function in Angular.js because it's not working on objects. Is there a way to merge a nested object into its parent using Javascript? For example, I have the following data structure: { "data" : [ { "ch ...