- javaScript
- 变量
- let
- const
- var
- 注释
- // 单行注释
- /多行注释/
- 运算符
- 位运算符
- 赋值运算符
- var age = 18, address = “张家界”, gz = 2000;
- = += -= *= %=
- var [x,y,z] = [‘hello’, ‘JavaScript’, ‘ES6’];
console.log(x,y,z); // hello JavaScript ES6 - var person = {
name: ‘小明’,
age: 20,
gender: ‘male’,
passport: ‘G-12345678’,
school: ‘No.4 middle school’
};
var {name, age, passport} = person; - var x=1, y=2;
[x, y] = [y, x]
- 算术运算符
```
- 变量
/ %
- 逻辑运算符- && || !- 布尔运算符> = < <=- NaN === NaN; // false- == ===- ==```javascript先检查两个操作数的数据类型是否相同如果相同,则比较两个数是否相等如果不同,则先将两个数转换为相同数据类型,再进行比较
先检查两个操作数的数据类型是否相同若不同,直接返回false若相同,则比较二者是否相等
- ===
- 条件运算符
- 1>2? “正确”:”错误”;
- 数据类型
- 数据种类
- Null
- Undefined
- NaN
- Number
- String
多行 字符串- 格式化
- var name = ‘明明’;
var message =你好,${name };
console.log(message);
- var name = ‘明明’;
- 方法
- 获取长度
- message.length;
- 索引字符
- message[0]
- 获取字符位置
- message.indexOf(“你”);
- 根据区间索引字符
- message.substring(0, 3)
- 获取长度
- Bool
- Array
- 创建
- [1, 2, 3.14, ‘Hello’, null, true]
- 更改长度
- arr.length=2; // [ 1, 2 ]
- arr.length=8; // [ 1, 2, 3.14, ‘Hello’, null, true, <2 empty items> ]
- 更改元素
- arr[0] = 99; //[ 99, 2, 3.14, ‘Hello’, null, true ]
- 方法
- 索引位置
- arr.indexOf(99) // 0
- 切片
- arr.slice(0, 3) // [ 99, 2, 3.14 ]
arr.slice(3) // [ ‘Hello’, null, true ] arr.slice() // 复制
- arr.slice(0, 3) // [ 99, 2, 3.14 ]
- 添加元素
- unshift
- push
- 删除元素
- shift
- pop
- 排序
- arr.sort();
- arr.reverse();
- 同时添加和删除
- var array = [‘Microsoft’, ‘Apple’, ‘Yahoo’, ‘AOL’, ‘Excite’, ‘Oracle’];
- 索引位置
- 创建
- 数据种类
var org = array.splice(2, 3, “Google”, “Facebook”);
console.log(org); //[ ‘Yahoo’, ‘AOL’, ‘Excite’ ]
console.log(array); //[ ‘Microsoft’, ‘Apple’, ‘Google’, ‘Facebook’, ‘Oracle’ ]
var onlyDel = array.splice(2, 2);
console.log(onlyDel); // [ ‘Google’, ‘Facebook’ ]
var onlyAdd = arr.splice(2, 0, “Google”, “FackBook”);
console.log(onlyAdd); // []
console.log(array);
- concat- var arr = ["A", "B", "C"];
var added = arr.concat([1, 2, 3]);
console.log(added); // [ ‘A’, ‘B’, ‘C’, 1, 2, 3 ]
- join- var arr = ['A', 'B', 'C', 1, 2, 3];
console.log(arr.join(“—-“)); // A—-B—-C—-1—-2—-3
- Obj- 创建- var xiaoming = {name: '小明',birth: 1990,school: 'No.1 Middle School',height: 1.70,weight: 65,score: null
};
- 获取属性- xiaoming.name- 添加属性- xiaoming.hibbit = "吃零食";- 删除属性- delete xiaoming.age- 检查属性是否存在- 'age' in xiaoming- map- var m = new Map([['Michael', 95], ['Bob', 75], ['Tracy', 85]]);
console.log(m.get(‘Bob’));
-
var map = new Map();
map.set(“adam”,67);
console.log(map); // Map { ‘adam’ => 67 }
console.log(map.has(“adam”)); //true
map.delete(“adam”);
console.log(map); //Map {}
- set- var set = new Set([1,2,3,3]);
console.log(set); // Set { 1, 2, 3 }
set.add(4);
console.log(set); // Set { 1, 2, 3, 4 }
set.delete(4);
console.log(set);
- 类型判断- typeof //确定类型- instanceof //判断类型- 类型转换- obj.toString()- parseInt(obj) parseFloat(obj) // 传入进制参数可以进行进制转换- 流程控制- 条件分支- if- if else- if else if else- 循环- for- for (i=0; i<arr.length; i++) {do......something
}
- var x = 0;
for (;;){
if (x > 100) {
break;
}
x++;
console.log(x)
}
- for(var key in obj){
do……something
}
- var array = [1, 2, 3];
for(var element of array){
console.log(element)
}
- var array = [“A”,”B”,”C”];
array.forEach(function (element,index,array) {
console.log(element + ‘, index = ‘ + index,’array=’+array);
});
- while- while- do while- switch- var BLUE = "blue", RED = "red", GREEN = "green";
var sColor = BLUE;
switch (sColor) {
case BLUE: alert(“Blue”);
break;…
- 函数- 自定义- function abs1(x) {if (x > 0) {return x;} else {return -x;}
}
- var abs2 = function (x) {if (x >= 0) {return x;} else {return -x;}
};
- 子主题 3
- arguments- function foo(x) {console.log(`x=` + x);for (var i = 0; i < arguments.length; i++) {console.log(arguments[i])}
}
- rest- function restDemo(a, b, ...rest) {console.log(rest)
}
restDemo(1, 2, 3, 4, 5); //[ 3, 4, 5, 6, 7 ]
- 方法(对象中)- function(){ }- this指向- function getAge() {const y = new Date().getFullYear();return y - this.birth;
}
var xiaoming = {
name: ‘小明’,
birth: 1990,
age: getAge
};
console.log(getAge.apply(xiaoming,[]));
console.log(getAge.call(xiaoming));
// apply会参数打包成数组
- 装饰器- 高阶函数- 自定义- var heighfunc = function (x, y, f) {return f(x) + f(y)
};
var f = function (n) {
return n * n;
};
console.log(heighfunc(5, 6, f));
- 内部- map- var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var results = arr.map(pow);
console.log(results);
- reduce- var array = [1,3,5,7,9];
console.log(array.reduce(function (x, y) {
return x + y;
}));
- filter- var array = [1, 2, 4, 5, 6, 9, 10, 15];
var r = array.filter(function (x) {
return x % 2 === 0;
});
console.log(r);
- sort- var array = [10, 20, 30];
array.sort(function (x, y) {
if (x > y) {
return -1;
} else {
return 1;
}
});
console.log(array);
- every- var arr = ['Apple', 'pear', 'orange'];
console.log(arr.every(function (s) {
return s.length > 0;
})); // true, 因为每个元素都满足s.length>0
- find- var arr = ['Apple', 'pear', 'orange'];
console.log(arr.find(function (s) {
return s.toLowerCase() === s;
})); // ‘pear’, 因为pear全部是小写
- findIndex- var arr = ['Apple', 'pear', 'orange'];
console.log(arr.findIndex(function (s) {
return s.toLowerCase() === s;
})); // 1, 因为’pear’的索引是1
- forEach- var arr = ['Apple', 'pear', 'orange'];
arr.forEach(console.log);
- 闭包- var iBaseNum = 10;
function addNum(iNum1, iNum2) {
function doAdd() {
return iNum1 + iNum2 + iBaseNum;
}
return doAdd();
} // doAdd() 函数根本不接受参数,它使用的值是从执行环境中获取的
- 箭头函数- var fn = x => x * x;- 生成器- function*gener() {for (let i = 0; i < 100; i++) {yield i;}
}
var gen = gener();
console.log(gen.next());
console.log(gen.next());
console.log(gen.next().value);
- function fib(max) {
var
t,
a = 0,
b = 1,
arr = [0, 1];
while (arr.length < max) {
[a, b] = [b, a + b];
arr.push(b);
}
return arr;
}
- 作用域- 就近原则- 内层函数变量- 嵌套层变量- 函数外变量- 标准对象- 包装对象- var n = new Number(123); // 123,生成了新的包装类型
var b = new Boolean(true); // true,生成了新的包装类型
var s = new String(‘str’); // ‘str’,生成了新的包装类型
- Date- RegExp- Json- JSON.stringify(obj)- JSON.parse(“text”)- 面向对象- 原型- class- class Student {constructor(name) {this.name = name;}hello() {console.log('Hello,' + this.name + '!')}
}
var student = new Student(“小明”);
student.hello();
class PrimaryStudent extends Student {
constructor(name, grade) {
super(name);
this.grade = grade;
}
myGrade() {
console.log(“I am grade” + this.grade);
}
}
var primaryStudent = new PrimaryStudent(“小明”,”31”);
primaryStudent.hello();
primaryStudent.myGrade();
- 浏览器对象- alert("弹窗测试")- prompt("请输入你的年龄")- confirm(“确认框测试”)- window- navigator- screen- location- document- 文档对象- 异常处理- try ... catch- try ... catch ... finally- throw- 包管理工具
