Changeset 33291 for main


Ignore:
Timestamp:
2019-07-03T18:42:59+12:00 (5 years ago)
Author:
ak19
Message:

JavaScript has no block level vars, only function level and (file) globals. To make a confusing part of existing map-scripts.js easier to read for programmers who are used to block level var declarations, have rewritten some existing code to be more explicit in showing that variables declared in within a function are finction level scoped rather than block level scoped, as it's otherwise easy to mistake what's going on.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/web/interfaces/default/js/map-scripts.js

    r33290 r33291  
    571571        return;
    572572    }
    573    
     573
     574    var doc;
    574575    var visibleMarkers = new Array();
    575576    for(var i = 0; i < _docList.ids.length; i++)
    576577    {
    577         var doc = _docList.getDocByIndex(i);
     578        //var doc = _docList.getDocByIndex(i);
     579                     // NOTE: in JavaScript, "local" vars have function scope, not mere block level scope. Wherever declared inside a function, they get hoisted to function top.
     580                     // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
     581                     // But this feature is confusing, so have now declared var doc nearer to function top, at function level so outside current local block, and am assigning here:
     582        doc = _docList.getDocByIndex(i);
    578583        if(typeof doc.shapes !== 'undefined') {
    579584            for(var x = 0; x < doc.shapes.length; x++) {
     
    608613        }
    609614
    610         var doc = visibleMarkers[_docList.loopIndex];
    611         elem = gs.jqGet("div" + doc.nodeID); // TODO: this used to redefine elem by doing var elem = <....>
     615        //var doc = visibleMarkers[_docList.loopIndex]; // See NOTE above.
     616        doc = visibleMarkers[_docList.loopIndex];
     617        elem = gs.jqGet("div" + doc.nodeID); // This used to redefine elem by doing var elem = <....>
     618        // This worked because "If you re-declare a JavaScript variable, it will not lose its value." See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
    612619        if(elem.length)
    613620        {
     
    617624        _docList.loopIndex ++;     
    618625    }   
    619     //console.log("@@@@ DOC:");
    620     //console.log(doc);
    621    
     626
     627    _gsDebug("@@@ loopThroughmarkers() - DOC:", doc);
     628   
    622629    if(doc.marker) {
    623630        doc.marker.markerInfo.open(_map, doc.marker); // TODO: how does doc have a value here? Where is the value set at this block level?
Note: See TracChangeset for help on using the changeset viewer.