第一章中,JavaScript的入门知识有哪些?

2026-05-17 10:251阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1358个文字,预计阅读时间需要6分钟。

1. 探索JavaScript,尝试少量JS代码,可打开浏览器开发者工具(F12、Ctrl+Shift+I、Command+Option+I),也可下载Node后打开终端,输入node回车,输入JS代码。

2.Hello World,运行长段代码时。

1. 探索JavaScript
  1. 尝试少量JS代码,可以打开浏览器的Web开发者工具(F12、Ctrl+Shift+I、Command+Option+I)
  2. 也可以下载Node后,打开终端,输入node并回车,输入JS代码

2. Hello World

当需要运行长段JS代码时,可以将代码写进JS文件中,在node运行这个文件或者将js文件引入html文件,适用url地址在浏览器打开该html文件


3. JavaScript之旅
  1. 第2章: 解释JavaScript注释、分号和Unicode字符集
  2. 第3章: 解释JavaScript变量以及可以赋给这些变量的值
  3. 第4章: 表达式
  4. 第5章: 语句
  5. 第6章: 对象
  6. 第7章: 数组
  7. 第8章: 函数
  8. 第9章: 面对对象编程
  9. 第10章: 模块
    展示文件或脚本中的JavvaScript代码如何使用其他文件和脚本中定义的JavaScript函数
  10. 第11章: JavaScript标准库
    展示所有JavaScript程序都可以使用的内置函数和类,包括像映射、集合这样重要的数据结构,还有用于文本模式匹配的正则表达式类,以及序列化JavaScript数据结构的函数,等等。
  11. 第12章: 迭代器与生成器
    解释for/of循环的原理,以及如何定义可以在for/of中使用的类。该章还介绍生成器函数及yield语句。
  12. 第13章: 异步JavaScript
    该章深入讨论JavaScript的异步编程,涵盖回调与事件、基于期约的API,以及asyncawait关键字虽然核心JavaScript语言并非异步的,但浏览器和Node中的API默认都是异步的。该章解释适用这些API的技术。
  13. 第14章: 元编程
    介绍一些高级JavaScript特性,为其他JavaScript程序员编写代码库的读者可能会感兴趣。
  14. 第15章: 浏览器中的JavaScript
    介绍浏览器宿主环境,解释浏览器如何执行JavaScript代码,涵盖浏览器定义的大多数重要API。该章是迄今为止这本书中最长的一章。
  15. 第16章: Node服务器端JavaScript
    介绍Node宿主环境,涵盖基础编程模型、数据结构和需要理解的最重要的API。
  16. 第17章: JavaScript工具和扩展
    涵盖广泛应用并有效提升开发者效率的工具及语言扩展。

4. 示例:字符频率柱形图

/** * 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 this.defaultValue = defaultValue; // Remember the default value } get(key) { if (this.has(key)) { // If the key is already in the map return super.get(key); // return its value from superclass. } else { return this.defaultValue; // Otherwise return the default value } } } // This class computes and displays letter frequency histograms class Histogram { constructor() { this.letterCounts = new DefaultMap(0); // Map from letters to counts this.totalLetters = 0; // How many letters in all } // This function updates the histogram with the letters of text. add(text) { // Remove whitespace from the text, and convert to upper case text = text.replace(/\s/g, "").toUpperCase(); // Now loop through the characters of the text for(let character of text) { let count = this.letterCounts.get(character); // Get old count this.letterCounts.set(character, count+1); // Increment it this.totalLetters++; } } // Convert the histogram to a string that displays an ASCII graphic toString() { // Convert the Map to an array of [key,value] arrays let entries = [...this.letterCounts]; // Sort the array by count, then alphabetically entries.sort((a,b) => { // A function to define sort order. if (a[1] === b[1]) { // If the counts are the same return a[0] < b[0] ? -1 : 1; // sort alphabetically. } else { // If the counts differ return b[1] - a[1]; // sort by largest count. } }); // Convert the counts to percentages for(let entry of entries) { entry[1] = entry[1] / this.totalLetters*100; } // Drop any entries less than 1% entries = entries.filter(entry => entry[1] >= 1); // Now convert each entry to a line of text let lines = entries.map( ([l,n]) => `${l}: ${"#".repeat(Math.round(n))} ${n.toFixed(2)}%` ); // And return the concatenated lines, separated by newline characters. return lines.join("\n"); } } // This async (Promise-returning) function creates a Histogram object, // asynchronously reads chunks of text from standard input, and adds those chunks to // the histogram. When it reaches the end of the stream, it returns this histogram async function histogramFromStdin() { process.stdin.setEncoding("utf-8"); // Read Unicode strings, not bytes let histogram = new Histogram(); for await (let chunk of process.stdin) { histogram.add(chunk); } return histogram; } // This one final line of code is the main body of the program. // It makes a Histogram object from standard input, then prints the histogram. histogramFromStdin().then(histogram => { console.log(histogram.toString()); }); 注:在编译器上使用node charfreq.js < charfreq.js会报错,是因为编译器终端选择的是powershell而不是cmd,可以通过更改编译器终端或使用cmd打开代码来解决这个问题。


5. 小结

本书以自底向上的方式解释JavaScript。这意味着要先从较低层次的注释、标识符、变量和类型讲起,然后在此基础上介绍表达式、语句、对象和函数。接着介绍更高层次的语言抽象,例如类和模块。本书的书名包含“权威”二字是认真的,接下来的章节对这门语言的解释可能详细得令人反感。然而,想要真正掌握JavaScript必须理解这些细节,希望你能花时间从头到尾读完这本书。不过,不要一上来就想着这样做。假如某一节内容你怎么也看不懂,可以先跳过去。等你对这门语言有了一个整体的了解时,可以再回来了解那些细节。

本文共计1358个文字,预计阅读时间需要6分钟。

1. 探索JavaScript,尝试少量JS代码,可打开浏览器开发者工具(F12、Ctrl+Shift+I、Command+Option+I),也可下载Node后打开终端,输入node回车,输入JS代码。

2.Hello World,运行长段代码时。

1. 探索JavaScript
  1. 尝试少量JS代码,可以打开浏览器的Web开发者工具(F12、Ctrl+Shift+I、Command+Option+I)
  2. 也可以下载Node后,打开终端,输入node并回车,输入JS代码

2. Hello World

当需要运行长段JS代码时,可以将代码写进JS文件中,在node运行这个文件或者将js文件引入html文件,适用url地址在浏览器打开该html文件


3. JavaScript之旅
  1. 第2章: 解释JavaScript注释、分号和Unicode字符集
  2. 第3章: 解释JavaScript变量以及可以赋给这些变量的值
  3. 第4章: 表达式
  4. 第5章: 语句
  5. 第6章: 对象
  6. 第7章: 数组
  7. 第8章: 函数
  8. 第9章: 面对对象编程
  9. 第10章: 模块
    展示文件或脚本中的JavvaScript代码如何使用其他文件和脚本中定义的JavaScript函数
  10. 第11章: JavaScript标准库
    展示所有JavaScript程序都可以使用的内置函数和类,包括像映射、集合这样重要的数据结构,还有用于文本模式匹配的正则表达式类,以及序列化JavaScript数据结构的函数,等等。
  11. 第12章: 迭代器与生成器
    解释for/of循环的原理,以及如何定义可以在for/of中使用的类。该章还介绍生成器函数及yield语句。
  12. 第13章: 异步JavaScript
    该章深入讨论JavaScript的异步编程,涵盖回调与事件、基于期约的API,以及asyncawait关键字虽然核心JavaScript语言并非异步的,但浏览器和Node中的API默认都是异步的。该章解释适用这些API的技术。
  13. 第14章: 元编程
    介绍一些高级JavaScript特性,为其他JavaScript程序员编写代码库的读者可能会感兴趣。
  14. 第15章: 浏览器中的JavaScript
    介绍浏览器宿主环境,解释浏览器如何执行JavaScript代码,涵盖浏览器定义的大多数重要API。该章是迄今为止这本书中最长的一章。
  15. 第16章: Node服务器端JavaScript
    介绍Node宿主环境,涵盖基础编程模型、数据结构和需要理解的最重要的API。
  16. 第17章: JavaScript工具和扩展
    涵盖广泛应用并有效提升开发者效率的工具及语言扩展。

4. 示例:字符频率柱形图

/** * 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 this.defaultValue = defaultValue; // Remember the default value } get(key) { if (this.has(key)) { // If the key is already in the map return super.get(key); // return its value from superclass. } else { return this.defaultValue; // Otherwise return the default value } } } // This class computes and displays letter frequency histograms class Histogram { constructor() { this.letterCounts = new DefaultMap(0); // Map from letters to counts this.totalLetters = 0; // How many letters in all } // This function updates the histogram with the letters of text. add(text) { // Remove whitespace from the text, and convert to upper case text = text.replace(/\s/g, "").toUpperCase(); // Now loop through the characters of the text for(let character of text) { let count = this.letterCounts.get(character); // Get old count this.letterCounts.set(character, count+1); // Increment it this.totalLetters++; } } // Convert the histogram to a string that displays an ASCII graphic toString() { // Convert the Map to an array of [key,value] arrays let entries = [...this.letterCounts]; // Sort the array by count, then alphabetically entries.sort((a,b) => { // A function to define sort order. if (a[1] === b[1]) { // If the counts are the same return a[0] < b[0] ? -1 : 1; // sort alphabetically. } else { // If the counts differ return b[1] - a[1]; // sort by largest count. } }); // Convert the counts to percentages for(let entry of entries) { entry[1] = entry[1] / this.totalLetters*100; } // Drop any entries less than 1% entries = entries.filter(entry => entry[1] >= 1); // Now convert each entry to a line of text let lines = entries.map( ([l,n]) => `${l}: ${"#".repeat(Math.round(n))} ${n.toFixed(2)}%` ); // And return the concatenated lines, separated by newline characters. return lines.join("\n"); } } // This async (Promise-returning) function creates a Histogram object, // asynchronously reads chunks of text from standard input, and adds those chunks to // the histogram. When it reaches the end of the stream, it returns this histogram async function histogramFromStdin() { process.stdin.setEncoding("utf-8"); // Read Unicode strings, not bytes let histogram = new Histogram(); for await (let chunk of process.stdin) { histogram.add(chunk); } return histogram; } // This one final line of code is the main body of the program. // It makes a Histogram object from standard input, then prints the histogram. histogramFromStdin().then(histogram => { console.log(histogram.toString()); }); 注:在编译器上使用node charfreq.js < charfreq.js会报错,是因为编译器终端选择的是powershell而不是cmd,可以通过更改编译器终端或使用cmd打开代码来解决这个问题。


5. 小结

本书以自底向上的方式解释JavaScript。这意味着要先从较低层次的注释、标识符、变量和类型讲起,然后在此基础上介绍表达式、语句、对象和函数。接着介绍更高层次的语言抽象,例如类和模块。本书的书名包含“权威”二字是认真的,接下来的章节对这门语言的解释可能详细得令人反感。然而,想要真正掌握JavaScript必须理解这些细节,希望你能花时间从头到尾读完这本书。不过,不要一上来就想着这样做。假如某一节内容你怎么也看不懂,可以先跳过去。等你对这门语言有了一个整体的了解时,可以再回来了解那些细节。