简体   繁体   English

Chrome上的Math.random()javascript函数* undefined *

[英]Math.random() javascript function *undefined* on Chrome

In first place, I am convinced that this is a trivial question, but I cannot understand why this is happening and I couldn't find an answer anywhere else. 首先,我确信这是一个微不足道的问题,但我不明白为什么会这样,我无法在其他任何地方找到答案。 I googled my issue with little success, but if I missed something and just wasted your time please point me in the right direction and accept my humble excuses. 我用Google搜索我的问题但收效甚微,但是如果我错过了什么,只是浪费了你的时间,请指出我正确的方向并接受我谦虚的借口。

That said, here is what happens. 也就是说,这就是发生的事情。 I am building a simple script to display a graph drawn by a distributed algorithm (giraph) and I am working on Linux. 我正在构建一个简单的脚本来显示由分布式算法(giraph)绘制的图形,我正在使用Linux。 I import all the libraries and, in particular, jQuery and everything works in Firefox (version 36.0.1). 我导入了所有库,特别是jQuery,一切都在Firefox(版本36.0.1)中工作。 Switching to Chrome (version 41.0.2272.89 (64-bit)), the page stops working. 切换到Chrome(版本41.0.2272.89(64位)),页面停止工作。 Investigating the issue, I found out that the error was inside the jQuery.extend() function into the jQuery library, at the following line: 调查这个问题,我发现错误是在jQuery库中的jQuery.extend()函数内,在以下行:

expando: "jQuery" + ( version + Math.random() ).replace( /\D/g, "" ),

In fact, trying to invoke Math.random() into the browser console leads to: 实际上,尝试将Math.random()调用到浏览器控制台会导致:

> Math.random();
> Uncaught TypeError: Undefined is not a function

Anyway, while typing the console autocompletes the "Math" variable, with the following result: 无论如何,在键入控制台时自动完成“Math”变量,结果如下:

> Math
> function (){return "";}

This does not happen with Chrome on MacOS X Mavericks. Chrome在MacOS X Mavericks上不会发生这种情况。 Did anybody experience this kind of behaviour before? 以前有人经历过这种行为吗?

EDIT: Unfortunately, I don't think that this is a namespace conflict. 编辑:不幸的是,我认为这不是命名空间冲突。 In my library I import only one other library (other than jQuery), which is Sigma js . 在我的库中,我只导入了另一个库(jQuery除外),这是Sigma js As suggested, I wrote Math in the console and tried to understand which library did override Math (using "Show Function Definition"), with the following result, taken from a file named "VM53" (which I did not write and/or linked directly). 正如所建议的那样,我在控制台中编写了Math,并尝试了解哪个库覆盖了Math(使用“显示函数定义”),结果如下,取自名为“VM53”的文件(我没有编写和/或链接直)。

(function (){
    for (var i in window)
    {
    try {
            var jsType = typeof window[i];
            switch (jsType.toUpperCase())
            {                   
                case "FUNCTION": 
                    if (window[i] !== window.location)
                    {
                        if (window[i] === window.open || (window.showModelessDialog && window[i] === window.showModelessDialog))
                            window[i] = function(){return true;};
                        else if (window[i] === window.onbeforeunload)   // To try to fix onbeforeunload pop ups some users report seeing but I can't replicate.
                            window.onbeforeunload = null;
                        else if (window[i] === window.onunload)
                            window.onunload = null;                             
                        else
                            window[i] = function(){return "";};
                    }
                    break;                          
            }           
        }
        catch(err)
        {}      
    }

    for (var i in document)
    {
        try {
            var jsType = typeof document[i];
            switch (jsType.toUpperCase())
            {                   
                case "FUNCTION":
                    document[i] = function(){return "";};
                    break;                  
            }           
        }
        catch(err)
        {}      
    }

    try {
        eval = function(){return "";};              
        unescape = function(){return "";};
        String = function(){return "";};
        parseInt = function(){return "";};
        parseFloat = function(){return "";};
        Number = function(){return "";};
        isNaN = function(){return "";};
        isFinite = function(){return "";};
        escape = function(){return "";};
        encodeURIComponent = function(){return "";};
        encodeURI = function(){return "";};
        decodeURIComponent = function(){return "";};
        decodeURI = function(){return "";};
        Array = function(){return "";};
        Boolean = function(){return "";};
        Date = function(){return "";};
        Math = function(){return "";};
        Number = function(){return "";};
        RegExp = function(){return "";};

        var oNav = navigator;
        navigator = function(){return "";};
        oNav = null;            
    }
    catch(err)
    {}

})();

Something overrides Math in your code. 有些东西会覆盖代码中的Math

Execute Math in your chrome console. 在Chrome控制台中执行Math Execute context menu on it and select "Show function definition". 在其上执行上下文菜单,然后选择“显示功能定义”。 This will probably lead you to "Sources" panel and show script where it is being overriden. 这可能会引导您进入“Sources”面板并显示正在覆盖它的脚本。

EDIT: this is chrome extension called notscripts . 编辑:这是名为notscripts chrome扩展名。 Disable it. 禁用它。 Here is the source code for proof: webarchive 以下是证明的源代码: webarchive

Another library on your page is overriding the Math object. 页面上的另一个库将覆盖Math对象。 This is one of the reasons that polluting the Global Namespace is frowned upon. 这是污染全球命名空间的原因之一。

For reference Math should return the following 作为参考, Math应返回以下内容

在此输入图像描述

Somehow, NotScript got automatically installed in my Chrome browser on Ubuntu 16.04. 不知何故, NotScript自动安装在Ubuntu 16.04上的Chrome浏览器中。 That inserts the script you show in your update just after the <html> tag. 这会在<html>标记之后插入您在更新中显示的脚本。

I had to go to the settings, look at the extensions, disable the NotScripts and then restart Chrome to it would be gone. 我不得不去设置,查看扩展,禁用NotScripts ,然后重启Chrome,它就会消失。

Chromium Extensions显示NotScripts 0.9.6已启用

On Ubuntu, this must be very recent or the script got updated recently because I ran the exact same test in Chrome, with the exact same scripts, a few days ago, and it was working just fine. 在Ubuntu上,这必须是最近的,或者脚本最近更新了因为我在Chrome中运行完全相同的测试,几天前使用完全相同的脚本,并且它运行得很好。

Interestingly enough, it looks like it was removed 2 years ago (Nov 2014) and maybe re-added recently? 有趣的是,看起来它在2年前 (2014 11月)被删除了,并且最近可能会重新添加?

I found another situation that could cause this to happen. 我发现了另一种可能导致这种情况发生的情况。 I was converting a PHP function to a JS function and I had the following line: 我正在将PHP函数转换为JS函数,我有以下行:

ticks += "," . Math.floor(yyy * intMax);

Changing it to 把它改成

ticks += "," + Math.floor(yyy * intMax);

solved the problem 解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM