简体   繁体   English

Javascript hasOwnProperty在Google Chrome浏览器下不起作用

[英]Javascript hasOwnProperty does not work under Google Chrome

I am currently working on a project with some help and it has been going well, until this incident. 我目前正在一个项目的帮助下进行,并且进展顺利,直到发生此事件。

  function runCommand(commandString)
  {
   commands = new Object();
   commands.clear = function(){ $('#terminal').html('') }

   parameters = commandString.split(" ");
   command = parameters.shift();
   if( commands.hasOwnProperty(command)){
    commands[command](parameters);
   }
   else
   {
    $('#terminal').append(command+' command not recognized.'+'<br>');
   }
  }

The person who was helping me made this function, so I could run the "terminal-like" browsers that I needed to work on. 帮助我的人完成了此功能,因此我可以运行需要使用的“终端式”浏览器。

It works fine when using Firefox, heres an example: 使用Firefox时效果很好,下面是一个示例:

guest@shell:/$ sudo make me sandwich
sudo command not recognized.
guest@shell:/$ clear

*clears*

guest@shell:/$ clear

But under google chrome this happen: 但是在谷歌浏览器下会发生这种情况:

guest@shell:/$ sudo make me sandwich
sudo command not recognized.
guest@shell:/$ clear
clear command not recognized.

I believe that it has something to do with "commands.hasOwnProperty(command)" that is preventing it from working properly. 我认为它与“ commands.hasOwnProperty(command)”有关,导致它无法正常工作。

I am using JQuery the javascript library to build the website, and I need to know how to solve this problem, or an alternative. 我正在使用JQuery的JavaScript库来构建网站,并且我需要知道如何解决此问题或替代方法。

I very highly doubt Chrome has any problems with hasOwnProperty, but in either case I cleaned up your code and changed the hasOwnProperty check into a typeof check, which should also be slightly more robust: 非常怀疑Chrome的hasOwnProperty是否存在任何问题,但是无论哪种情况,我都清理了您的代码并将hasOwnProperty检查更改为typeof检查,该检查也应该更加健壮:

var runCommand = (function () {
    var terminal = $('#terminal');

    var commands = {
        clear: function () {
            terminal.html('');
        }
    };

    return function (commandString) {
        var parameters = commandString.split(" ");
        var command = parameters.shift();

        if (typeof commands[command] === "function") {
            commands[command](parameters);
        } else {
            terminal.append(command + ' command not recognized.<br />');
        }
    };
}());

...in case you're curious, I'm storing the commands object in a closure, that way it only needs to be created once, rather than being created/destroyed every time the function is run. ...如果您好奇的话,我会将命令对象存储在一个闭包中,这样,它只需创建一次即可,而不是每次运行该函数时都会创建/销毁它。 This is dramatically faster, obviously. 显然,这大大加快了速度。

Also, I'm only running a single search for the #terminal element, then storing it in a variable and using that. 另外,我只对#terminal元素进行一次搜索,然后将其存储在变量中并使用它。 This is also for performance, since running a jQuery search is much more expensive than storing the result and using it repeatedly. 这也是为了提高性能,因为运行jQuery搜索比存储结果并重复使用它要昂贵得多。

Works for me in Chrome 3.0.195.38. 在Chrome 3.0.195.38中为我工作。

I have never heard of any problems with hasOwnProperty in Chrome. 我从未听说过Chrome中hasOwnProperty有任何问题。 I suspect your problem lies elsewhere. 我怀疑您的问题出在其他地方。 Full test case? 完整的测试用例? How are you reading the commands in? 您如何阅读其中的命令? Is it possible you are getting a command with leading whitespace in Chrome? 您是否可能在Chrome中使用领先的空格获取命令?

You should remember to declare local variables in your function ( commands , parameters etc.) as var , otherwise you are getting accidental globals, which can produce odd and hard-to-debug errors, though I'd doubt that was the problem here. 您应该记得将函数( commandsparameters等)中的局部变量声明为var ,否则会出现意外的全局变量,这可能会产生奇数且难以调试的错误,尽管我怀疑这是问题所在。

I bet the problem is with differences in how "split" works between Firefox and Chrome.I would try changing the way that you get the parameters and the command: 我敢打赌,问题在于Firefox和Chrome之间“拆分”的工作方式存在差异。我将尝试更改获取参数和命令的方式:

var command = commandString, parameters = [];
if (/ /.test(command)) {
  parameters = command.split(' ');
  command = parameters[0];
  parameters = parameters.slice(1);
}

edit on second thought that should make no difference; 编辑应该没有什么区别的第二个想法; as far as I can tell Safari should handle the simple "split on space" thing just fine. 据我所知,Safari应该能够处理简单的“在空间上分割”的事情。 Make sure that you add some debugging around that code to see what "command" is when the function thinks it can't find "clear" in the object. 确保在该代码周围添加了一些调试功能,以在函数认为无法在对象中找到“清除”时查看“命令”是什么。 And as @bobince says, define your variables with var !!!! 就像@bobince所说的那样, var !!!! 定义变量

Using hasOwnProperty on a DOM Object instance will now return false 现在在DOM Object实例上使用hasOwnProperty将返回false

More info.. 更多信息..

function runCommand(commandString) {
    var terminal = $('#terminal'), //cached to reduce calls to jQuery
    commands = {    //start object literal, continue var statement
        clear: function (){ 
            terminal.html(''); 
        }
    },
    parameters = commandString.split(' '),
    command = parameters.shift(); //end var statement
    if (commands.hasOwnProperty(command)) {
        commands[command].call(parameters);
        console.log(command + ' command sent.'); //output for testing
    } else {
        terminal.append(command + ' command not recognized.<br>');
        console.warn(command + ' command not recognized.'); //output for testing
    }
}

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

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