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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:37:57+00:00 2026-05-26T20:37:57+00:00

I’m trying to improve my Javascript coding style and have been reading that it’s

  • 0

I’m trying to improve my Javascript coding style and have been reading that it’s good to namespace stuff.

However I can’t seem to use the “this” keyword everywhere that I would like to – instead I can only access my class properties via the namespace (in this case “oldMap”) from within anonymous functions. This means that I can’t change the namespace id without changing the code too – this seems like it’s probably wrong.

Here’s the class I built – it actually appears to work properly though. (sorry it’s quite long).

Any suggestions / hints on what I’m doing right / wrong gratefully received. Thanks

var oldMap = {

    map : null,

    center : {lat:50, lng:20, zoom:3}, 

    drawn : false,

    data : {},

    divId : "oldMap",

    url : "php/getMapData.php",

    infowindow : new google.maps.InfoWindow({ 
        size: new google.maps.Size(150,50)
    }),

    init : function () {

        if (!this.drawn){
            $.getJSON(
                this.url,
                function(d){
                    if(d){
                        $.extend(oldMap.data,d);

                        var latlng = new google.maps.LatLng(oldMap.center.lat, oldMap.center.lng);
                        var myOptions = {
                            zoom: oldMap.center.zoom,
                            center: latlng,
                            mapTypeId: google.maps.MapTypeId.TERRAIN
                        };

                        // create the map
                        map = new google.maps.Map(document.getElementById("oldMap"),myOptions);

                        // create the legend
                        var legendDiv = document.createElement('DIV');
                        legendDiv.innerHTML = '<div id="legend"><img src="images/markers/legend-blur.png"></div>';
                        map.controls[google.maps.ControlPosition.TOP_LEFT].push(legendDiv);

                        google.maps.event.addListener(map, 'click', function() {
                            infowindow.close();
                        });

                        // Set the info window html template
                        var infoWindowTemplate = "<div id='balloon'>{{#url2}}<img src='{{url2}}' />{{/url2}}<h2>{{project_name}}</h2><p><b>Amount</b> &euro; {{cost}}</p><p><b>Country</b> {{country}}</p><p><b>Year</b> {{year}}</p><p><b>Project Type</b> {{project_type}}</p><p>{{description}}</p>{{#url}}<p><a target='_blank' href='{{url}}'>More info</a></p>{{/url}}</div>"

                        // loop through the projects
                        for(var m in oldMap.data) {

                            // if the project has a marker type defined
                            if (oldMap.data[m].marker) {

                                // point
                                var point = new google.maps.LatLng(oldMap.data[m].lat, oldMap.data[m].longtd); 

                                // create HTML for info window
                                var infoHtml = Mustache.to_html(infoWindowTemplate, oldMap.data[m]);

                                // icon
                                var icon = new google.maps.MarkerImage(
                                    "images/markers/33px/" + oldMap.data[m].marker + ".png",
                                    new google.maps.Size(33,33)
                                );

                                // create a marker for this project
                                var marker = oldMap.createMarker(point,infoHtml,icon);
                            }
                        }

                        oldMap.drawn = true;
                    }
                }
            )
        }
    },


    createMarker : function (latlng, html, icon) {

        // create the marker
        var marker = new google.maps.Marker({
            position: latlng,
            icon: icon,
            map: map,
            zIndex: Math.round(latlng.lat()*-100000)<<5
        });

        // open info window when marker clicked
        google.maps.event.addListener(marker, 'click', function() {
            oldMap.infowindow.setContent(html); 
            oldMap.infowindow.open(map,marker);
        });
    }

};
  • 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-26T20:37:58+00:00Added an answer on May 26, 2026 at 8:37 pm

    First line of your functions directly on the object should be…

    function () {
        var that = this;
        ...
    }
    

    Then, in your inner functions, swap references to this with that.

    This is because inner functions’ this points to window.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.