【问题标题】:Can't declare variable as object property in JavaScript无法在 JavaScript 中将变量声明为对象属性
【发布时间】:2017-04-17 15:15:13
【问题描述】:

我声明了一个函数,当给定一个字符串作为参数时,它返回一个对象,其中键是字符串中的单词。值是该单词在字符串中出现的次数。

当我运行以下代码时,我得到 {}。

function countWords(string) {
  var result = {};
  var words = string.split(' ');
  for (var i = 0; i < words.length; i++) {
    var currentElement = words[i];
    var prop = result[currentElement];
    if(prop) {
        prop += 1;
    } else {
        prop = 1;
    }
  }
  return result;
}

console.log(countWords('hello hello')); // => {'hello': 2}

但是,将 prop = 1 替换为 result[CurrentElement] = 1 会返回预期的答案。

为什么在这种情况下使用 prop = 1 是错误的?

【问题讨论】:

标签: javascript


【解决方案1】:

var prop = result[currentElement]; 这会将result[currentElement] 中的值复制到prop,然后添加一个但永远不要将其放回数组中。赋值是按值,而不是按引用。

【讨论】:

    【解决方案2】:

    您需要使用对对象的引用进行检查和递增。

    result[currentElement]
    ^^                      object
          ^^^^^^^^^^^^^^^^  property of object
    

    如果你只是得到它的值,你会得到一个原始值,而不是想要的对象引用。

    value = result[currentElement] // undefined in the first call
                                   //         1 in the second call
    

    但您不再有对结果的引用。

    function countWords(string) {
      var result = {};
      var words = string.split(' ');
      for (var i = 0; i < words.length; i++) {
        var currentElement = words[i];
        if (result[currentElement]) {
          result[currentElement] += 1;
        } else {
          result[currentElement] = 1;
        }
      }
      return result;
    }
    
    console.log(countWords('hello hello')); // => {'hello': 2}

    【讨论】:

      【解决方案3】:

      在这一行:

      var prop = result[currentElement];
      

      您正在复制 result[currentElement] 的值(显然是一个数字),然后增加副本,因此原始值 (result[currentElement]) 保持不变。

      【讨论】:

        【解决方案4】:

        因为变量几乎从不通过引用与其他任何东西链接。

        其他方式只有两种情况:

        1. arguments 处于非严格模式。

          function f(a) {
            console.log(a, arguments[0]);
            a = 10;
            console.log(a, arguments[0]);
          }
          
          function g(a) {
            'use strict';
            console.log(a, arguments[0]);
            a = 10;
            console.log(a, arguments[0]);
          }
          
          f(7);
          g(7);
        2. 赋值左侧,包括分解。

          var a = 8, b = 10;
          console.log(a, b);
          [a, b] = [b, a];
          console.log(a, b);
          
          var x = [0, 1, 2];
          console.log(x.join(" "));
          [x[1], x[0], x[2]] = x;
          console.log(x.join(" "));

        【讨论】:

          【解决方案5】:

          你需要像这样在结果中添加它:

          <script>
              function countWords(string) {
            var result = {};
            var words = string.split(' ');
            for (var i = 0; i < words.length; i++) {
              var currentElement = words[i];
              var prop = result[currentElement];
              if(prop) {
                  prop += 1;
              } else {
                  prop = 1;
              }
              result[currentElement]=prop;
            }
            return result;
          }
          
          console.log(countWords('hello hello test khalil'));
          </script>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-01-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多