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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:24:29+00:00 2026-06-09T18:24:29+00:00

I often see this pattern to define javascript objects function Person(name) { this.name =

  • 0

I often see this pattern to define javascript objects

function Person(name) {
    this.name = name;
}
Person.prototype.describe = function () {
    return "Person called "+this.name;
};

And in this article it says that adding properties directly to the prototype objct is considered an anti-pattern.

Coming from “classical class based” languages, having to define the properties apart from methods doesn’t sound quite right, moreoever in javascript, where a method should be just a property with a function value (am I right here?)

I wanted to know if anybody can explain this, or even suggest a better way to handle these situations

  • 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-09T18:24:31+00:00Added an answer on June 9, 2026 at 6:24 pm

    In usual object-oriented languages, you have a definition of the class describing members, methods and the constructor.

    In JS, the definition of the “class” (it is not really class like in other languages… sometimes the term pseudoclass is used) is the constructor itself. If your object is parametrised by name, it makes sense to write

    function Person(name) {
        this.name = name;
    }
    

    i.e. the property name must be set in the constructor.

    Of course, you can write

    function Person(name) {
        this.name = name;
        this.describe = function() { ... };
    }
    

    and it will work as you expect.

    However, in this case you are creating a separate instance of the method with every call of the constructor.

    On the other hand, here:

    Person.prototype.describe = function () {
        return "Person called "+this.name;
    };
    

    you only define the method once. All instances of Person will receive a pointer (called __proto__ and not accessible by programmer in most browsers) to Person.prototype. So if you call

    var myPerson = new Person();
    myPerson.describe();
    

    it will work, because JS looks object members directly in the object, then in its prototype etc. all the way up to Object.prototype.

    The point is that in the second case, only one instance of the function will exist. Which you will probably agree that is a better design. And even if you don’t, it simply takes less memory.

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

Sidebar

Related Questions

I see often this pattern (function(){}).call(this) Is that the same with this one? (function(that){})(this)
In classes that implement INotifyPropertyChanged I often see this pattern : public string FirstName
I see this pattern used very often! Click on a link popup appears with
I see this type of pattern ( found this example here ) quite often
I'm not really sure how to express this, but one pattern I often see
I often see the code which uses mock in Rspec, like this: describe GET
I often see JavaScript code where a function may take in an options object
I often see things like this in rails views: <% form_tag some_path do -%>
I often see the code like this: public abstract class AbstractDataReader { public void
I don't see this problem too often but I've got a .cshtml that uses

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.