JavaScript 入门笔记

                     

贡献者: addis

预备知识 HTML 基础

   JavaScript 常见于网页中,一般浏览器都可以运行。我们以 Chrome 浏览器为例演示 Hello World 程序。

<!DOCTYPE html>
<html>
	<body>
		<div id = a></div>
		<script>
            console.log("hello world 1");
            window.alert("hello world 2");
			document.getElementById("a").innerHTML = "hello world 3";
            document.write("hello world 4");
		</script>
	</body>
</html>
把这段程序复制到一个文本文件,并命名为 test.html,用 Chrome 打开即可自动运行。按 F12 可打开调试窗口和命令行。

   <script>...</script> 中的 4 行程序就是 JavaScript,它们这里分别演示了用 4 种不同的方法显示 “hello world”:

  1. 输出到命令行(需要 F12 才能看到)
  2. 弹出提示窗
  3. 插入到 <div>...</div> 元素中
  4. 插入到 <script> 之前

   在用 Chrome 调试时,可以用 F12 打开调试窗口,可以实现逐行调试,查看错误和警告信息和命令行输出等。

1. 用 VScode 调试 Javascript

2. 常识

算符

3. 变量

字符串

数组

4. 判断循环

5. 函数

6. 常用函数

7. 对象

   定义成员函数,可以使用关键词 this

const person = {
  firstName: "John",
  lastName : "Doe",
  id       : 5566,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};

   Constructor 的例子(参考

function Person(first, last, age, eyecolor) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
  this.eyeColor = eyecolor;
  this.name = function() {
    return this.firstName + " " + this.lastName;
  };
}

8. 模块

9. 容器

set

10. math.js

   math.js 和内建的 Math 库兼容(可以一起用)。注意这不是一个模块,不需要 import,可以用 file:// 协议。

   在代码前面插入 <script src="math.js"></script>,也可以是 url 如 https://cdnjs.cloudflare.com/ajax/libs/mathjs/10.0.2/math.js

常用

复数

线性代数

                     

© 小时科技 保留一切权利