JavaScript 基础:词法结构详解
1. 字符频率直方图示例
在深入探讨 JavaScript 的词法结构之前,先来看一个有趣的示例:字符频率直方图的计算。这个示例程序可以读取标准输入的文本,计算其中每个字符的出现频率,并以直方图的形式输出结果。
/**
* This Node program reads text from standard input, computes the frequency
* of each letter in that text, and displays a histogram of the most
* frequently used characters. It requires Node 12 or higher to run.
*
* In a Unix-type environment you can invoke the program like this:
* node charfreq.js < corpus.txt
*/
// This class extends Map so that the get() method returns the specified
// value instead of null when the key is not in the map
class DefaultMap extends Map {
constructor(defaultValue) {
super(); // Invoke superclass constructor
超级会员免费看
订阅专栏 解锁全文
35

被折叠的 条评论
为什么被折叠?



