Skip to main content

红宝书

2: 在 html 中使用 JavaScript

行内 js VS 外链 js

外链 js,节省下载资源(如果存在多个引用)。 外链 js 可以设置 defer & async 属性。行内 js 就算设置了也没用。

defer VS async

defer:相当于放在 html 的最后加载,单个的时候保证顺序。 会在 DomContentLoaded 之前执行。 async: 遇到就下载,下载完就执行。不保证顺序。

都能在 onload 事件之前加载&执行完成。

3: 基本概念

标识符

[a-zA-Z], _, $,只能是这三种开头。

数据类型(8)

简单数据类型

  1. String
  2. Boolean
  3. Number
  4. null
  5. undefined
  6. BigInt
  7. Symbol

复杂数据类型

  1. Object

typeof 的结果(8)

const str = 'aliveAmy';
const isFemale = true;
const birthday = 719;
const hobbies = ['reading', 'painting'];
const hairCount = BigInt(1000000);
const uniqueId = Symbol('id');
const coding = () => 'amy is a software engineer.';
const status = null;
let age;
const address = NaN;

console.log(typeof str); // string
console.log(typeof isFemale); // boolean
console.log(typeof birthday); // number
console.log(typeof hobbies); // object
console.log(typeof hairCount); // bigint
console.log(typeof uniqueId, uniqueId); // symbol
console.log(typeof coding); // function
console.log(typeof status); // object
console.log(typeof age); // undefined
console.log(typeof address); // number

函数的参数

思考: 为什么函数可以传与定义时不符的参数呢? 比如定义函数的时候定义了两个参数,你可以传三五个,也可以不传。 原因: 函数内部,是用一个数组来表示参数的,你可以用 arguments 对象(类数组)来获得。箭头函数中则不行。

function 的长度

第四章

第五章

第六章

第七章

第八章