【问题标题】:Global variable keep old value全局变量保持旧值
【发布时间】:2016-11-21 00:12:48
【问题描述】:

发生了一些奇怪的事情......我使用 Phaserjs 并尝试覆盖 Device 类:

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'; 

为什么?问题接缝似乎是范围问题,但我不知道是什么!

【问题讨论】:

  • 旧值是多少?
  • 我在 FF,所以我必须有 Firefox: TRUE 和 iOS:FALSE 但我得到 FF: FALSE 和 iOS : true ,这是不合逻辑的!!!
  • 浏览器检测失败的原因有很多,包括插件。 navigator.userAgent 是最近才出现的,所以它并不总是有效
  • 你可以在这里联系phaser的创建者github.com/photonstorm/phaser/issues

标签: javascript firefox scope phaser-framework


【解决方案1】:

如果不知道旧值的设置位置,这是不可能回答的。 Device 对象仅包含根据 navigator.userAgent 返回真/假值的函数(而非变量),所以您确定是否在任何地方设置了任何旧值?

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    相关资源
    最近更新 更多