笔记索引
数据类型
属于内置对象的一部分
Number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 "use strict" ; var x = 1 ; 1 / 3 === 1 - 2 / 3 ; 123 ; 0.456 ; 1.2345e3 ; -99 ; 0o10 ; 0b11 ; NaN ; Infinity ;
布尔
1 2 3 4 true === false ;isNaN (NaN );var a = null ;
可迭代 itrable
数组、键值对、集合可以用 forEach 方法遍历元素
1 2 3 4 5 6 a.forEach (function (element, index, array ) { console .log (element + ", index = " + index); });
数组
注意越界访问
1 2 var arr = [1 , 2 , 3.14 , "Hello" , null , true ]; arr.length ();
键值对
1 2 3 4 5 var m = new Map ([ ["Michael" , 95 ], ["Bob" , 75 ], ["Tracy" , 85 ], ]);
集合
1 var s = new Set ([1 , 2 , 3 , 3 , "3" ]);
对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 var person = { name : "Bob" , age : 20 , tags : ["js" , "web" , "mobile" ], city : { Beijing : "Capital" }, hasCar : true , zipcode : null , "middle-school" : "No.1 Middle School" , function : function name (params ) {}, get getter () {}, super : super .name (); }; person.age ; person["middle-school" ]; person.zip ; delete person.city ; "hasCar" in person;
字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 ('I\'m "OK"!' ); ("\x41" ); ("\u4e2d\u6587" ); `这是一个 多行 字符串` ;var str = "小明" ;var age = 20 ;var message = `你好, ${str} , 你今年${age} 岁了!` ; String .length ();name[0 ];
作用域
1 2 3 4 5 6 var a = 2 ; let b = 16 ; const c = 15 ;
变量提升
var 变量的声明会被提到代码最前方,初始化和调用则不会
表达式
1 2 3 4 5 6 7 8 9 10 11 12 this function class function * yield yield * async function await [] {} /ab+c/i ()
左表达式
左边的值是赋值的目标
属性访问符
1 2 3 new super ([arguments ]); super .functionOnParent ([arguments ]);
一元运算符
一元运算符只有一个操作数.
1 2 3 4 delete expression; void expression; typeof expression; ~expression;
关系运算符
比较运算符比较二个操作数并返回基于比较结果的 Boolean 值
=>
不是运算符,而是箭头函数的表示符
1 2 Number in Array ; Number instanceof Object ;
相等运算符
1 2 3 4 a == b; c != d; null === null ; a !== a;
二元逻辑运算符
逻辑运算符典型的用法是用于 boolean(逻辑)值运算, 它们返回 boolean 值
1 2 true && false ; false || true ;
三元运算符
1 condition ? ifTrue : ifFalse;
解构赋值
1 2 3 4 var [x, y, z] = ["hello" , "JavaScript" , "ES6" ]; var [, , z] = ["hello" , "JavaScript" , "ES6" ]; var { name, single = true } = { name : "小明" , x : 100 , y : 200 }; ({ x, y } = { name : "小明" , x : 100 , y : 200 });
语句与声明
流程控制
条件
JavaScript 把 null、undefined、0、NaN 和空字符串’'视为 false,其他值一概视为 true
1 2 3 4 5 6 7 if (person.age >= 18 ) { alert ("adult" ); } else if (age >= 6 ) { alert ("teenager" ); } else { alert ("kid" ); }
循环
for
for in 依 Index 循环,常用于对象
for of 依 内容 循环,多用于数组等可迭代对象
forEach 依 内容 循环方法,多用于数组等可迭代对象
for await…of 异步循环,存在兼容问题
1 2 3 4 5 var x;var i;for (i = 1 ; i <= 10000 ; i++) { x = x + i; }
while
1 2 3 4 5 var n;while (n > 0 ) { x = x + n; n = n - 2 ; }
switch
1 2 3 4 5 6 7 8 9 10 11 12 switch (key) { case value1 : case value2 : break ; case value : break ; default : break ; }
do while
do 内至少会执行一遍
1 2 3 4 5 6 7 8 9 loop1 : do { n = n + 1 ; continue loop1; } while (n < 100 ); break ; continue ; Empty ;
变量声明
var 变量,会被提升,拥有函数作用域
let 块级变量
const 常量
其他
1 2 3 4 5 6 7 8 9 10 11 12 13 debugger ; import {} from "module" ;export let name1, name2; export { name1, name2, nameN }; export { variable1 as name1, variable2 as name2, nameN }; export const { name1, name2 : bar } = o; export default expression; export * from "module" ;
面向对象
基于类
类声明不会受到变量提升影响,且默认严格
本质是基于原型的语法糖
声明
声明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 class Student { constructor (name ) { this .name = name; this .age = super .age (); } hello ( ) { alert (`Hello, ${this .name} !` ); } static bye ( ) { alert (`Bye, ${this .name} !` ); } rebonjour ( ) { this .bye (); } get value () { return this .name ; } async aurevoir ( ) { return ; } *generator ( ) { yield ; } async *asyncGenerator ( ) {} }
类表达式
1 2 3 4 5 6 let Rectangle = class { constructor (height, width ) { this .height = height; this .width = width; } };
1 2 3 4 5 6 let Rectangle = class Rectangle2 { constructor (height, width ) { this .height = height; this .width = width; } };
继承
1 2 3 4 5 6 7 class PrimaryStudent extends Student { constructor (name, grade ) { super (name); super .hello (); this .grade = grade; } }
Mix-in
没有多继承,但可以通过继承声明新的类,在其中添加
1 2 3 4 5 6 7 8 9 10 11 12 var firstMixin = (Base ) => return class extends Base { first ( ) {} }; var secondMixin = (Base ) => return class extends Base { second ( ) {} }; class Foo {}class Bar extends firstMixin (secondMixin (Foo )) {}
基于原型
与基于类 JS 不区分对象与类,所有对象都可以作为原型
使用,实例化类似于继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 function Student (name ) { this .name = name; this .hello = function ( ) { alert ("Hello, " + this .name + "!" ); }; } let me = new Student ("我" ); Student .prototype .hello = function ( ) { alert ("Hello, " + this .name + "!" ); };
原型继承
原型继承,强行构造原型链
1 2 3 4 5 6 7 8 9 10 function inherits (Child, Parent ) {var F = function ( ) {}; F.prototype = Parent .prototype ; Child .prototype = new F (); Child .prototype .constructor = Child ; } ···