views:

34

answers:

1

this is my demo code :

<body onload="initialize()">

<script>
function initialize(){
    var d='adddd'
    $.getScript('other.js', function() {
        a()
    });
}   
</script>
</body>

and this is the demo other.js :

function a(){
    alert(d)
}

then , you will be find a error :

d is not defined

so you have to do this :

function a(d){
        alert(d)
    }

and

a(d)

if we have many Variable,we have to add them one by one , like this:

a(d,e,f,r,f,g,,h,e,w)

that is my Nightmare, so how to load another js file not use to add them ,

the next is my original code :

    <script type="text/javascript">
        function initialize() {
            var n=0;
            var latlng = new google.maps.LatLng(-34.397, 150.644);
            var myOptions = {
              zoom: 8,
              center: latlng,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("map_canvas"),
                myOptions);
            $.getScript('_lib/node.js', function() {
                console.log(node)
                var a = new node($('#test'),true);
                var b = new node($('#tabs_'));
            });

        }
    </script>

the node.js is :

    function node(obj,is_add_class_name) {
        this.n=0;
        this.setMap(map);
        this.obj=obj;

        this.is_add_class_name=is_add_class_name;
        var me=this;
        $('.delete').click(function(){
            if($('.delete',me.div_)[0]==this){
                me.onRemove()
            }
        })
        map.setOptions({
            draggable:true
        })
    }
    node.prototype = new google.maps.OverlayView();
    node.prototype.onAdd = function() {
        var div = $(this.obj)
        div.show();
        this.div_ = div[0];
        this.addPolygon(new google.maps.LatLng(-34.397, 150.644));
        var panes = this.getPanes();
        panes.overlayLayer.appendChild(div[0]);
    }
    node.prototype.onRemove = function() {
        this.div_.parentNode.removeChild(this.div_);
        this.div_ = null;
    }

but the error is :

map is not defined

the node.js is separated from the main document ,now i have to call node function use this :

var a = new node($('#test'),true,map,..other variables in main file );

that is very Complex, so my question is how to load a js file simply ,don't use to send every variables the js needed .

thanks

A: 

What you seem to be wanting are global variables. These are usually frowned upon because their use can cause severe problems.

In JavaScript if you declare your variables outside a function they are global and are accessible by any other script.

So your sample code would be:

<body onload="initialize()">

<script>
var d='adddd';
function initialize(){
    $.getScript('other.js', function() {
        a()
    });
}   
</script>
</body>

Where I have simply moved the declaration of d outside the function so that it can be referenced by any other script.

So why is this considered bad practice? Well, if you are combining many scripts from different sources and each one declares its own global variables, then there is no way to ensure that they do not step on each other. Further JavaScript has its own global variables which if you step over will result in difficult to find bugs.

If you must use globals, then give them a name which indicates in which script they are defined, e.g. script1_d in your example. That way you make sure that globals have unique names and you know which one you are referencing.

Hans B PUFAL
thanks very musch
zjm1126