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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:23:37+00:00 2026-05-23T23:23:37+00:00

I’ve been broadening my horizons learning javascript and I have a quick question about

  • 0

I’ve been broadening my horizons learning javascript and I have a quick question about style. I used to use a literal notation when making my code like this.

var foo = {
  bar:function(){/*something*/}

};

but I would have problems accessing certain variables within the object, it would claim a few didn’t exist nomatter what. So I started using this, cause it worked.

var foo = {};
foo.bar = function(){/*something*/};

But after reading around a bit I get the impression that this style isn’t preferred. Not that I liked the style, it just got things to work. Is there any best practices when using the literal notation so all my variables and such will work?

Wish I had the old code that didn’t work still, but I think it was a variable pointing to another variable withing the same object literal or something like that.

  • 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-23T23:23:38+00:00Added an answer on May 23, 2026 at 11:23 pm

    Object literals should be a static definition, the moment you do calculations or logic in the literal declaration it will break. If you need logic/calculations use an object constructor instead.

    For example

    var o = {
      b: 4,
      c: 8, 
      d: o.b + o.c
    }
    

    Will cause a TypeError because o is undefined. The reason for this is that the object literal is created first and then assigned to the variable o.

    So whilst your creating the object literal, the object o doesn’t exist.

    The following :

    var o = {
      b: 4,
      c: 8, 
      d: this.b + this.c
    }
    

    Will work, but not like you expect it to. You cannot reference the object literal directly in the declaration. Because this is bound to window in this example and not to o.

    If you want to do logic use a constructor

    var o = new function() {
      this.b = 4;
      this.c = 8;
      this.d = this.b + this.c;
    }
    

    or

    var O = function() {
      this.b = 4;
      this.c = 8;
      this.d = this.b + this.c;
    }
    var o = new O();
    

    Conclusion:

    As long as the object literal declaration contains no logic it is safe. (Just declare functions and properties with static values).

    If the declaration contains logic it should be in a function body (like a constructor)

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

Sidebar

Related Questions

No related questions found

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.