访问全局变量的问题

时间:2011-08-22 15:35:52

标签: javascript html

我在访问javascript中附加到窗口的全局变量时遇到了一些问题。 这是我的代码片段,我使用脚本标记嵌入到我的索引页面中,然后说

var Geolocation = function() {
  var self = this;

  self.errors = {
    TIMEOUT: 1,
    POSITION_UNAVAILABLE: 2,
    PERMISSION_DENIED: 3,
    UNKNOWN_ERROR: 4
  };

  self.getCurrentPosition = function( success, error, options ) {
    // simulate the wait for the user choice
    var geoIntervalId = window.setInterval(function( ) {
      if ( state != null ) {
        window.clearInterval(geoIntervalId);

        switch( state ) {
          case 'ok':
            success(new Position(latitude, longitude));
            break;
          case 'timeout': case 'position_unavailable': case 'permission_denied':
            error( new GeolocationError( self.errors[state.toUpperCase()] ) );
            break;
          default:
            error( new GeolocationError( self.errors.UNKNOWN_ERROR ) );
        }
      }
    }, 100); // ms
  };
}

var Position = function( lat, lng ) {
  this.coords = new Coordinates(lat, lng);
}

var Coordinates = function( lat, lng ) {
  this.latitude  = lat;
  this.longitude = lng;
}

var GeolocationError = function( code ) {
  this.TIMEOUT = 1;
  this.POSITION_UNAVAILABLE = 2;
  this.PERMISSION_DENIED = 3;
  this.UNKNOWN_ERROR = 4;

  this.code = code;
}

var state     = null,
    latitude  = null,
    longitude = null;

window.geolocation_provider = new Geolocation();

在此之后,我使用脚本标记再次在索引文件中包含另一个javascript文件说test.js。现在,当我尝试访问window.geolocation_provider变量时,它变为null。为什么会如此

2 个答案:

答案 0 :(得分:1)

问题可能是某些文件在其他文件之前/之后执行;理想情况下,通过确保在执行任何重要代码之前加载所有javascript函数定义来解决此问题。

您可能需要查看javascript中的Module pattern。还有一些其他问题可以解决这个问题,such as this one

答案 1 :(得分:0)

试试这个

(function(window){
  var Geolocation = function() {
  var self = this;

  self.errors = {
    TIMEOUT: 1,
    POSITION_UNAVAILABLE: 2,
    PERMISSION_DENIED: 3,
    UNKNOWN_ERROR: 4
  };

  self.getCurrentPosition = function( success, error, options ) {
    // simulate the wait for the user choice
    var geoIntervalId = window.setInterval(function( ) {
      if ( state !== null ) {
        window.clearInterval(geoIntervalId);

        switch( state ) {
          case 'ok':
            success(new Position(latitude, longitude));
            break;
          case 'timeout': case 'position_unavailable': case 'permission_denied':
            error( new GeolocationError( self.errors[state.toUpperCase()] ) );
            break;
          default:
            error( new GeolocationError( self.errors.UNKNOWN_ERROR ) );
        }
      }
    }, 100); // ms
  };
};

var Position = function( lat, lng ) {
  this.coords = new Coordinates(lat, lng);
};

var Coordinates = function( lat, lng ) {
  this.latitude  = lat;
  this.longitude = lng;
};

var GeolocationError = function( code ) {
  this.TIMEOUT = 1;
  this.POSITION_UNAVAILABLE = 2;
  this.PERMISSION_DENIED = 3;
  this.UNKNOWN_ERROR = 4;

  this.code = code;
};

var state     = null,
    latitude  = null,
    longitude = null;
if(window.geolocation_provider === undefined){
    window.geolocation_provider = new Geolocation();
}
})(window ? window : this)
相关问题