2016-11-21 64 views
1

奇怪的事情发生了....我用Phaserjs并尝试覆盖设备类:全局变量保持原值

let Device = (function(device) 
     { 
     return { 
      Android :function() { 
       return navigator.userAgent.match(/Android/i) == true; 
       //return Phaser.Device.Android != undefined; 
      }, 
      BlackBerry: function() { 
       return navigator.userAgent.match(/BlackBerry/i) == true; 
      }, 
      iOS: function() { 
       return navigator.userAgent.match(/iPhone|iPad|iPod/i) == true; 
       //return Phaser.Device.iOS == true; 
      }, 
      Opera: function() { 
       return navigator.userAgent.match(/Opera Mini/i) == true; 
      }, 
      Windows: function() { 
       return navigator.userAgent.match(/IEMobile/i) == true; 
       //return Phaser.Device.WindowsPhone != undefined; 
      }, 
      firefox : function() { 
       return navigator.userAgent.toLowerCase().indexOf('firefox') > -1; 
       //return Phaser.Device.Firefox == true; 
      }, 
      any: function() { 
       return (Device.Android() || Device.BlackBerry() || Device.iOS() || Device.Opera() || Device.Windows()); 
      }, 
      debug: function() 
      { 
       //console.log("Ios "+Phaser.Device.Firefox+" Dev:"+Phaser.Device); 
       return "Android:"+Device.Android()+" "+ 
        "BlackBerry:"+Device.BlackBerry()+" "+ 
        "iOS:"+Device.iOS()+" "+ 
        "Opera:"+Device.Opera()+" "+ 
        "Windows:"+Device.Windows()+" "+ 
        "firefox:"+Device.firefox(); 
      } 
      }; 
     })(Phaser.device); 
    export default Device; 

的Phaser.Device是独立的,并喜欢$(document).ready它需要首先初始化。该功能是Phaser.Device.whenReady(foobar,this);

问题是,当我致电Firefox或iOS的功能(Device.iOS())我得到一个旧值,而不是初始值(true和false)

我在主脚本导入装置这样:

import Device from './helpers/GameDevice'; 

为什么?问题接近是一个范围问题,但我不知道是什么!

+0

什么是旧价值? –

+0

我在FF,所以我必须有Firefox:TRUE和iOS:FALSE,但我得到FF:FALSE和iOS:true,这是不合逻辑的! – xeonarno

+0

浏览器检测可能由于多种原因而失败,包括附加。 navigator.userAgent是最近的事情,所以它并不总是工作 –

回答

0

如果不知道旧值的设置位置,这是不可能的。 Device对象只包含基于navigator.userAgent返回true/false值的函数(不是变量),那么你确定有任何旧值设置在任何地方?

另外,你有没有看过你正在使用的FireFox浏览器上的“navigator.userAgent”的字符串值?也许它根本不包含字符串firefox

从navigator.userAgent字符串中检测浏览器和操作系统非常棘手。多年来,navigator.userAgent的字符串已成为unwieldy and very cluttered,因为主要的浏览器已经从相同的代码库,浏览器的模仿,向后兼容性等

你可能想看看platform.js,而不是滚动您自己分叉。