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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:55:50+00:00 2026-05-27T00:55:50+00:00

I noticed that i can write functions like the one below to simulate classes.

  • 0

I noticed that i can write functions like the one below to simulate classes. I want to know if this is the most up-to-date way of doing it. Any comments appreciated. Thanks

function Apple(type) {
    this.type = type;
    this.color = "red";
    this.getInfo = function () {
        return this.color + ' ' + this.type + ' apple';
    };
}

var apple = new Apple('testapple');
apple.color = 'blue';
console.log(apple.getInfo());
  • 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-05-27T00:55:51+00:00Added an answer on May 27, 2026 at 12:55 am

    Your code works fine but not efficient enought as it gives each instance an getInfo function. This could be avoided. You could use the following patterns to simulate classes in JS.

    Basic Pattern

    To simulate a class property/method, you set properties/method on the Constructor function.

    function Apple() {};
    Apple.classProperty = some_value;
    Apple.classMethod = some_method;
    

    To simulate an instance property, you set inside the Constructor functions (as you did in your code):

    function Apple() {
      this.property = some_instance_value;
    };
    

    To simulate an instance method, you setup functions in the Constructor.prototype which will be shared across all its instances

    function Apple() {};
    Apple.prototype.instanceMethod = function () {...};
    

    Advanced Pattern

    If you want to set private/privileged method, Crockford has very useful patterns available.

    Private Method – only available to the constructor:

    function Constructor(...) {
    var that = this;
    var membername = value;
    function membername(...) {...}
    
    }
    

    Privileged Method – can access private method and is accesible to the public:

    function Constructor(...) {
    this.membername = function (...) {...};
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just noticed that you can do this in C#: Unit myUnit = 5;
In Visual Studio as most of you will have noticed that related file can
I noticed that you can call Queue.Synchronize to get a thread-safe queue object, but
I noticed that I can start a program with it's associated handler by writing
I noticed that you can't throw an exception in a destructor. So my question
I've noticed that you can manually recycle an application pool via IIS and that
I just noticed that you can not use standard math operators on an enum
I'm new to RubyAmf. I noticed that you can send an object with RubyAmf
Is there a way to convert HTML into XAML? I noticed that you can
I noticed today that auto-boxing can sometimes cause ambiguity in method overload resolution. The

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.