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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:35:41+00:00 2026-05-24T06:35:41+00:00

whats the best practice to use these? var x = { a: ‘a’, eat:

  • 0

whats the best practice to use these?

var x = { a: 'a', eat: function() { }, ... }

vs

var x = function() { var a = 'a'; this.eat = function() { }}

the above needs to be initiated:

new x();

can someone help me explain the importance of the two, and which one is preferred choice within the oop community? any word of wisdom would help. i also did some research but nothing came up. much thought is appreciated.

  • 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-24T06:35:42+00:00Added an answer on May 24, 2026 at 6:35 am

    The basic difference is that the first version exposes the variable ‘a’, while the second one hides it. So unless you want or need client code to access x.a, the second version is preferred.

    A third approach would be to use a prototype. In this case, a local variable in the constructor won’t do you much good, so if eat() needs access to a, then you’d write:

    function x() {
        this.a = 'a';
    }
    
    x.prototype.eat = function() {
        // do stuff with this.a
    }
    

    In this case, each instance has a new copy of a, but there’s only one copy of eat. The drawback (if you consider it one) is that a is available to users of x instances.

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

Sidebar

Related Questions

Best practice is to use unique ivs, but what is unique? Is it unique
What is the common way (or best practice) to optionally use JNI? E.g. I
Whats the best practice for implementing a confirmation page in .Net? I've used response.redirect(Thankyou.aspx)
I'm starting a new Titanium app and want to use best practices and avoid
What is best practice to use and distribute 3rd party jars? Approach one: Keep
What is the best practice/coding standard with regard to the this scope is AS3?
I've built a PHP app, and I've read that it's a best-practice to use
What are the best practices for using use cases to model system behavior that
What's the difference (if any) between model.__dict__['title_en'] and model.__getattribute__('title_en') and what's best practice ?
I'm faced with the following and I'm not sure what's best practice. Consider 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.