这段代码的语法是什么

What is the syntax of this block of code?

本文关键字:语法 是什么 代码 段代码      更新时间:2024-04-09

当我找到这个答案时,我正在寻找返回数组模式的方法:

var store = ['1','2','2','3','4'];
var frequency = {};  // array of frequency.
var max = 0;  // holds the max frequency.
var result;   // holds the max frequency element.
for(var v in store) {
    frequency[store[v]]=(frequency[store[v]] || 0)+1; // increment frequency.
    if(frequency[store[v]] > max) { // is this frequency > max so far ?
            max = frequency[store[v]];  // update max.
            result = store[v];          // update result.
    }
}

这段代码运行得很好,我对它的理解很好,所以我能够输出最常见的值和该值出现的次数。然而,这个代码块对我来说毫无意义。特别是这条线:

frequency[store[v]]=(frequency[store[v]] || 0)+1;

我认为频率是一个数组,store[v]作为索引。这个代码块中到底发生了什么?

这对你有用吗?

for(var v in store) {
    numberWeAreLookingAt = store[v];
    // we hadn't gone through this number yet, so it's not defined 
    // as a property in the object "frequency"
    if(frequency[numberWeAreLookingAt] === undefined)
      frequency[numberWeAreLookingAt] = 0; // let's initialize that property with the 
                                           // number zero, in it we will hold the
                                           // number of times it appeared
    // Sum 1 to the times it appeared already (or zero if we 
    // initialized it on the "if" above)
    frequency[numberWeAreLookingAt] = frequency[numberWeAreLookingAt] + 1;
    // the times this number appeared is more than "max"?
    if(frequency[numberWeAreLookingAt] > max) {
        // then now "max" is the times this number appeared
        max = frequency[numberWeAreLookingAt];
        // and the result is this number
        result = numberWeAreLookingAt;
    }
}

请注意,问题中的代码是完全可读的。如果你真的读不懂,你就不能"责怪程序员":你只是不太理解代码,应该好好学习。

"使代码可读"并不意味着使其冗长。。。它的意思是"让知道语法的人第一次读起来就很清楚",我认为你问题中的代码很适合。我的代码非常冗长,我的注释应该是多余的:它们只是在解释下一行代码的作用

唯一"可能"需要解释的代码行是:

frequency[store[v]]=(frequency[store[v]] || 0)+1;

你可以在上面看到它的分解。。。x = (x || 0)+1的意思是get x if it's defined, or 0 if it's not: then add 1 and assign back to x,这是我在代码中对详细形式所做的。

另一件似乎混淆OP的事情(如注释中所述)是使用括号语法来访问对象属性。这在动态执行的语言中并不罕见(我认为,考虑到对象原型是如何在javascript中完成的,括号语法比点语法更有意义,但这只是一个机会)。

在Javascript中,您可以使用两种不同的语法访问对象属性:object.property等效于object[property]。主要区别在于,当使用括号语法时,您可以使用表达式来计算属性名称(或者使用其他变量,就像我们在这里所做的那样)。例如,在C#中,可以使用dynamicExpandoObject执行相同的操作。

请注意,虽然这可能会让你感到困惑,但对这个问题来说并没有那么重要。。。你也可以把frequency想象成一个数组,其中索引器是对象,而不是序列号,它的工作原理是一样的(例如,在PHP中,你可以使用键控数组或C#中的Dictionary来实现这一点,这是函数式语言中非常典型的模式)。

frequency不是数组。它是一个对象,是关键点到值的映射。在这种情况下,键是store数组中的字符串,值是这些字符串出现的频率。查看正在发生的事情的最简单方法之一是添加几个console.logs:

var store = ['1','2','2','3','4'];
var frequency = {};  // array of frequency.
var max = 0;  // holds the max frequency.
var result;   // holds the max frequency element.
for(var v in store) {
    frequency[store[v]]=(frequency[store[v]] || 0)+1; // increment frequency.
    console.log( frequency );
    if(frequency[store[v]] > max) { // is this frequency > max so far ?
            max = frequency[store[v]];  // update max.
            console.log( 'Found new max!', max + ' occurrences of ''' + store[v] + '''' );
            result = store[v];          // update result.
    }
}

运行它并查看控制台,查看每次迭代后frequency对象包含的内容。这就是我得到的Chrome:

Object {1: 1}
Found new max! 1 occurrences of '1'
Object {1: 1, 2: 1}
Object {1: 1, 2: 2}
Found new max! 2 occurrences of '2'
Object {1: 1, 2: 2, 3: 1}
Object {1: 1, 2: 2, 3: 1, 4: 1}

frequency[store[v]]=(frequency[store[v]] || 0)+1;是的缩写

 if (frequency[store[v]]) {
     frequency[store[v]]++;
 } else {
     frequency[store[v]] = 1;
 }

这是因为JavaScript中的||运算符如果是truthy则返回其左操作数,否则返回其右操作数,因此当frequency[store[v]]undefined时(因为undefined为falsy),表达式frequency[store[v]] || 0的计算结果为0,但一旦frequency[store[v]]1或更大,它的计算结果将为frequency[store[v]]的值。

首先,浏览本教程,了解Javascript数组和Javascript对象之间的区别。然后你会读到这样的代码:

var store = ['1','2','2','3','4'];               // store[0] == '1', store[1] = '2', etc.
var frequency = {};  // array of frequency.      // frequency.'1' = undefined

然后,当任务发生时,就更容易理解了。frequency[store[v]]只是简单的frequency.'1',其中v == 0,这意味着您正在访问对象frequency及其由字符串1命名的字段。javascript对象可以有命名为任何名称的字段,例如frequency.'apple'frequency.'table'等。在给它们赋值之前,它们只是未定义的。

因此,任务更容易阅读

if (frequency.'1' is undefined)
    store the value [0 + 1] into frequency.'1'
else increment the value by 1