-
strict 模式
未使用 var 申明变量就使用的,将导致运行错误。
启用 strict 模式的方法是在 JavaScript 代码的第一行写上:"use strict";
数据类型
Number
JavaScript 不区分整数和浮点数,统一用 Number 表示,以下都是合法的 Number 类型:
1 | 123; // 整数123 |
字符串
1 | // 定义 |
布尔值
- true
- false
null
null 表示一个空的值。
undefined
undefined 表示值未定义。
数组
1 | // let nums = new Array(1, 2, 3, 4, 5); |
对象
对象是一组由键-值组成的无序集合。
1 | let cat = { |
Map
new Map()
– creates the map.map.set(key, value)
– stores the value by the key.map.get(key)
– returns the value by the key, undefined if key doesn’t exist in map.map.has(key)
– returns true if the key exists, false otherwise.map.delete(key)
– removes the value by the key.map.clear()
– removes everything from the map.map.size
– returns the current element count.
1 | let map = new Map(); |
Iteration over Map
map.keys()
– returns an iterable for keys,map.values()
– returns an iterable for values,map.entries()
– returns an iterable for entries [key, value], it’s used by default in for..of.
1 | let map = new Map(); |
Set
new Set(iterable)
– creates the set, and if an iterable object is provided (usually an array), copies values from it into the set.set.add(value)
– adds a value, returns the set itself.set.delete(value)
– removes the value, returns true if value existed at the moment of the call, otherwise false.set.has(value)
– returns true if the value exists in the set, otherwise false.set.clear()
– removes everything from the set.set.size
– is the elements count.
1 | let set = new Set([1, 2, 3, 4, 5, 3, 5]); |
声名变量
- var
- let
- const
let 的用法类似于 var,但是 let 只在所在的代码块内有效,所以一般使用 let 替代 var。而 const 用来声明常量。
运算符
operator | Meaning |
---|---|
== | Equality |
=== | Strict Equality |
!= | Inequality |
!== | Strict Inequality |
> | Greater Than |
>= | Greater or Equal Than |
< | Less Than |
<= | Less or Equal Than |
&& | And |
| | Or |
% | 取余数 |
NaN 这个特殊的 Number 与所有其他值都不相等,包括它自己,唯一能判断 NaN 的方法是通过 isNaN()函数。
==比较,它会自动转换数据类型再比较,很多时候,会得到非常诡异的结果
===比较,它不会自动转换数据类型,如果数据类型不一致,返回 false,如果一致,再比较。
由于 JavaScript 这个设计缺陷,不要使用==比较,始终坚持使用===比较
1 | console.log(NaN === NaN); //false |
循环
1 | // while |
函数
1 | function plusThree(num) { |
正则表达式
Character | Description |
---|---|
\ | Escapes a special character. |
| | Search for multiple patterns. To match “yes” or “no”, the regex is /yes |
i | This flag is used to ignore upper and lowercase. /ignorecase/i. |
g | Search or extract a pattern more than once. |
. | The wildcard character . will match any character except new lines. |
[] | Allow you to define the characters to match. /b[au]g/ will match “bag”, “bug” but not “bog”. |
[a-z] | Match all the characters between a and z. |
[1-9] | Match all the numbers between 1 and 9. |
[a-z1-9] | Match all the character between a and z, and the numbers between 1 and 9. |
[^] | Match the characters not in the set. [^a-e] match all other characters except A, B, C, D, and E. |
+ | Match 1 or more occurrences of the previous character in a row. |
* | Match 0 or more occurrences of the previous character. |
? | Match 0 or 1 occurrence of the previous character. Useful for Lazy matching. |
^ | Search for patterns at the beginning of strings. |
$ | Search for patterns at the end of a string. |
\w | Equal to [A-Za-z0-9_]. Matches upper, lowercase, numbers the and underscore character (-). |
\W | Matches any nonword character. Equivalent to [^a-za-z0-9_]. |
\d | Equal to [0-9]. Match one digit. |
\D | Equal to [^0-9]. Match one non digit. |
\s | Match a whitespace. |
\S | Match everything except whitespace. |
a{2,5} | Match the letter a between 3 and 5 times. |
a{2,} | Specify only the lower number of matches. |
a{5} | Specify the exact number of matches. |
(…) | Specify a group that can be acceded with number (from 1) |
- Regex Methods
Method | Description |
---|---|
test() | Returns true or false if the pattern match a string or not. |
match() | Extract the actual matches found. |
replace() | Search and replace text in a string . |
1 | // test method returns true or false if the pattern match a string or not |
面向对象
1 | let duck = { |
- 继承
1 | function Animal() {} |
- Mixins
1 | let bird = { |
- Closures to Protect Properties
1 | function Bird() { |
- Modules
1 | let funModule = (function() { |
ES6 面向对象
1 | class Book { |
- getters and setters
1 | class Book { |
- Statics Methods
1 | class Book { |
- Inheritance
1 | class Book { |
函数式编程
- map
- filter
- reduce
1 | let nums = [1, 2, 3, 4, 5, 6, 7, 8]; |
ES6 import and export
- import
1 | // we can choose which parts of a module or file to load into a given file. |
- export
1 | const capitalizeString = string => { |
查看类型
- type
客户端存储
- cookie
- Local Storage
1 | let map = new Map(); |