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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:54:19+00:00 2026-05-28T03:54:19+00:00

Using object literals for the first time and it seems I don’t quite understand

  • 0

Using object literals for the first time and it seems I don’t quite understand them right. What I need is private variables with the scope of the object and therefore accessible to all functions in the object literal.

Here’s the issue:

I have a simple object literal for a map object

var mapObj = {

    init: function (lat, lng, documentID) {
        var myOptions = {
            center: new google.maps.LatLng(lat, lng),
            zoom: 18,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById(documentID),
                myOptions);

        return map;
    },

    setMarker: function (lat, lng, map, contentObj) {
        var myLatlng = new google.maps.LatLng(lat, lng);
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: "Hello World!",
            content: contentObj
        });
    }
};

This is all fine, and I get to use it as mapObj.init(….) and mapObj.setMarker(…) which work perfectly.

What I want to be able to do is store some of these variable values – like ‘map’ in the init() method, and then have it accessible for other methods. For e.g. I should not need to pass ‘map’ in the setMarker, as the mapObj should hold the map internally after the init method.

Similarly I’d like to be able to do mapObj.getMap() to access the map object created in init.

I’m unable to figure out how it works. Declaring var map as

var mapObj {
   var map
}

for e.g throws errors.

Am I expecting this to work too much like C#? Should I be using the classic javascript ‘class’ constructions? Any pointers will help so I can move on.

Thanks

  • 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-28T03:54:20+00:00Added an answer on May 28, 2026 at 3:54 am

    Just wrap it in a function like so:

    var mapObj;
    !function() {
    
        var map;
    
        mapObj = {
    
            init: function (lat, lng, documentID) {
                var myOptions = {
                    center: new google.maps.LatLng(lat, lng),
                    zoom: 18,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                map = new google.maps.Map(document.getElementById(documentID),
                        myOptions);
    
                return map;
            },
    
            setMarker: function (lat, lng, map, contentObj) {
                var myLatlng = new google.maps.LatLng(lat, lng);
                var marker = new google.maps.Marker({
                    position: myLatlng,
                    map: map,
                    title: "Hello World!",
                    content: contentObj
                });
            }
        };
    }();
    

    Now map is only available inside that function (and all functions it contains). mapObj is available outside.

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

Sidebar

Related Questions

I am trying to access member variables of a class without using object. please
This is really weird. Its my first time with using the mustache library and
First time using the ASP.NET CreateUserWizard control and we are aliasing the Username Textbox
I'm just now getting into instantiating JavaScript object literals using a prototype constructor, but
I'm using object literals on my project. I'm targeting selecting with jquery. It works
How do I access the name of an item in an Object Literal using
if in a literal object i try to reference a function using this inside
In a project our team is using object lists to perform mass operations on
What is the difference between the following two snippets of code: using (Object o
I am building a web application and have been told that using object oriented

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.