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

  • SEARCH
  • Home
  • 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 9150091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:33:59+00:00 2026-06-17T11:33:59+00:00

I am trying to understand functions and modules in JavaScript better, specifically to understand

  • 0

I am trying to understand functions and modules in JavaScript better, specifically to understand the best way to create my own data types. I have the listing of 2 files below, Rectangle1.js and Rectangle2.js, and their sample output, which I have created to understand this better.

I’d like help from the community in helping me understand which of these (or which other way) is the best way to structure my code to create my own datatypes.

Rectangle1.js

function Rectangle(x,y,w,h) {
  width = w;
  height = h;

  this.area1 = function() {
    return width*height;
  }
}

Rectangle.prototype.area2 = function() {
  return width * height;
};

Rectangle.area3 = function() {
  return width * height;
}

exports.Rectangle = Rectangle;

Rectangle2.js

var RECTANGLE = (function(my) {
  function init(x,y,w,h) {
    this.w = w;
    this.h = h;
  }

  function area() {
    return this.w * this.h;
  }

  my.init = init;
  my.area = area;
  return my;
})(RECTANGLE || {});

exports.RECTANGLE = RECTANGLE;

SAMPLE INTERACTION

var r2 = require('Rectangle2.js');
r2.RECTANGLE.init(1,2,3,4);
r2.RECTANGLE // ...can see the private properties
r2.RECTANGLE.area() // returns 12

var r1 = require('Rectangle1.js');
r1 // shows area3 in r1
var rect = new r1.Rectangle(1,2,3,4);
rect // shows area1 in rect
rect.area1() // visible in methods, spits out 12
rect.area2() // not visible in methods, spits out 12
r1.Rectangle.area3() // not visible in rect, spits out 12
  • 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-17T11:34:00+00:00Added an answer on June 17, 2026 at 11:34 am

    The preferred way of structuring objects in javascript is closer to what you wrote in Rectangle1.js, though there are a few problems:

    First, when accepting arguments in the Rectangle(x,y,w,h) constructor function, you are not assigning them to instance variables, but rather to a somewhat unsafe global scope, which explains why Rectangle.area3() returns the same result. The this keyword allows you to assign and access instance variables. Keeping everything else the same, you would rather need to define the constructor like this (note the use of this.) :

    function Rectangle(x,y,w,h) {
    
      this.width = w;
      this.height = h;
    
      this.area1 = function() {
        return this.width*this.height;
      }
    }
    
    Rectangle.prototype.area2 = function() {
      return this.width * this.height;
    };
    

    Also, ideally, when you’re defining functions that would be reused identically across all instances of the same object, it’s a better practice to define them on Rectangle.prototype rather than on this in your constructor function. In the first case, only one Function object is created and is shared for all instances of Rectangle; in the latter, a new one is created for every instances of Rectangle.

    Structuring modules is fine the way you did it, given you’re targeting a javascript platform where you can organize code in CommonJS-like modules, like Node.js.

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

Sidebar

Related Questions

I am trying to understand how the Javascript client code of socket.io works. Specifically,
I'm trying to understand how to best use the JavaScript module pattern. My problem
I am trying different things to understand Decorators and functions in Python. Is the
I am trying to understand function pointers and am stuggling. I have seen the
I'm trying to understand how php function usort works. I have such code: <?php
I am trying understand ViewModels deeper and I have read many articles and blogs
Trying to understand what's the correct way of implementing OpenID authentication with Spring Security.
I am trying to understand the Python ctypes module. I have put together a
I'm trying to follow Organizing your application using Modules (require.js I'm struggling to understand
I'm using the rails-settings gem, and I'm trying to understand how you add functions

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.