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 8850219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:51:02+00:00 2026-06-14T12:51:02+00:00

Ok I should know the answer to this but for some reason I have

  • 0

Ok I should know the answer to this but for some reason I have never really understood or had the need to really get to know JavaScript.

My question is: Looking at the code samples below am I correct in my understanding or am I missing some information.


Sample 1

Need to instantiate the function (or class) in order to use the IsOld method, and a separate copy of the IsOld function will be created for each instance.

function MyClass1() {
    this.IsOld = function (age) {
        if (age > 40) {
            return true;
        }
        return false;
    };
}

// sample usage
var m1 = new MyClass1();
console.log(m1.IsOld(34));

Sample 2

Need to instantiate but unlike MyClass1 the scripting engine will not need to create a copy of the method IsOld for each class instance.

var MyClass2 = (function () {
    function MyClass2() { }

    MyClass2.prototype.IsOld = function (age) {
        if (age > 40) {
            return true;
        }
        return false;
    };

    return MyClass2;
})();

// sample usage
var m2 = new MyClass2();
console.log(m2.IsOld(34));

Sample 3

No need to instantiate the function / class to access the IsOld method. A single instance of the IsOld method is used across all invocations.

var MyClass3 = {
    IsOld: function (age) {
        if (age > 40) {
            return true;
        }
        return false;
    },
};

// sample uage
console.log(MyClass3.IsOld(34));

Note: I am guessing there are plenty of similar question / answers here on SO but for some reason I could not find one that actually made sense to me.

  • 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-14T12:51:04+00:00Added an answer on June 14, 2026 at 12:51 pm

    Your understandings seems to be correct.

    If by “need to instantiate” you mean use of ‘new’ keyword, I’d like to add something here.

    In JavaScript use of new keyword is not the only way to create new instances. (Edited as per comments)
    And any function can act as a constructor function.

    When you use ‘new’ keyword followed by any function (say ‘x’) what it does is

    1. Create new object (say ‘y’), and set the function x’s prototype as the new objects (y’s) prototype.
    2. Call the function ‘x’ in the newly created objects y’s context, i.e this would refer to this new object ‘y’ inside the function ‘x’
    3. If the function does not return an object, return the new object ‘x’ created by the new operator as the result of the new expression.

    Here is a good source for you to learn JavaScript by Douglas Crockford (http://javascript.crockford.com/)

    So, if you are concerned about the memory (you should be), use a constructor function, and add all the common methods to functions prototype like you have done in Sample 2.

    Then all those methods will be inherited to all the objects created using this function as the constructor. However, as mentioned before I think Sample 2 can be simpler:

    var MyClass2 = function MyClass2() { };
    
    MyClass2.prototype.IsOld = function (age) {
        if (age > 40) {
            return true;
        }
        return false;
    };
    
    var obj = new MyClass2();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is something that I'm sure I should know the answer to, but either
I know this shouldn't be that hard, but I couldn't find the answer on
This is one of those I probably should know this, but I don't questions.
This is something stupid I probably should know, but Googling fails me: When compiling
With this code I can download the file but I should know the file
This may seem like a dumb question, but still I don't know the answer.
I know I need to use the . something like .live to get this
I know this question could be similar to others but really I'm looking for
Should we be testing values that we already know the answer to? If a
First you should know I have looked into many questions and none of them

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.