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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:35:13+00:00 2026-05-27T20:35:13+00:00

I’m looking for the most optimized way to have an object in JS; since

  • 0

I’m looking for the most optimized way to have an object in JS; since JS allows you to do something in multiple ways I want to know which is considered to be “best practice”.

A conceptual use of the object would be this:

var John = new Person('John');
var Foo = new Person('Foo');

console.log(John.getName()); //Return 'John'
console.log(Foo.getName()); //Return 'Foo'
console.log(John.name); //undefined
console.log(Foo.name); //undefined

I have some examples here:

Example #1:

var Person = function (name) {
    this.getName = function () { return name; }
}

Here I actually have no properties; “name” cannot be changed or be read by public and getName method can be accessed public and invoke with local name variable. This actually meets my needs.

Example #2:

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

This has same behaviour as example #1. I’m just not sure about the difference between var method = function () {}; and function method() {}; from a functional and performance perspective.

Example #3:

var Person = function (name) {
    this.name = name;    
    Person.prototype.getName = function () { return this.name; }
}

Here I have name property which can be read/write by public and I also have a prototype getName method that can be accessed public.

Example #4:

var Person = function (name) {
    this.name = name;
    if (!Person._prototyped) {
        Person.prototype.getName = function () { return this.name; }
        Person._prototyped = true;
    }
}

Here I have what I had in example #3 with the difference that I make sure that prototype methods are set only once and only when required.

So the end question is, which of those you will suggest me to follow or if any of them then which way is the best practice?

  • 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-27T20:35:13+00:00Added an answer on May 27, 2026 at 8:35 pm

    For what you want, the best way is probably this:

    /**
     * @constructor
     */
    function Person(name) {
        this.getName = function() {
            return name;
        };
    }
    

    Why is this? Well, first of all, encapsulation. If possible, you generally want to put the function in the prototype, like so:

    /**
     * @constructor
     */
    function Person(name) {
        this.name = name;
    }
    
    Person.prototype.getName = function() {
        return this.name;
    };
    

    However! In this case, it appears you don’t want Person.name to be accessible. For that reason, you need to use closures.

    As for this method:

    var Person = function (name) {
        this.name = name;    
        Person.prototype.getName = function () { return this.name; }
    }
    

    That’s not good. You publicly expose this.name, so it offers no advantage over the previous prototype method, but you continually overwrite Person.prototype.getName with the same function. There’s no point in doing that; this will be the appropriate object every time. What’s more, Person.prototype.getName won’t be accessible until the first Person object is instantiated so there’s not much point in making it “public” in the first place. Your fourth example is basically identical, with more cruft making it harder to understand, but it still has all the disadvantages.

    So, in this case, go with the first version; when possible, though, set methods on prototype so they can be called outside of an instance.

    Finally, I would recommend using:

    function Person() {
    }
    

    over

    var Person = function() {
    }
    

    because 1) The second example is missing a semicolon, 2) the var implies that the constructor is variable which it should not be, and 3) I don’t believe JDoc allows @constructor to be applied to it, causing a warning in Closure Compiler at least1.


    1 Okay, not so important. But still…

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

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.