作用:与var类似,用于声明变量
特点:
<body>
<button>测试1</button>
<button>测试2</button>
<button>测试3</button>
</body>
<script type="text/javascript">
//console.log(age);// age is not defined
let age = 12;
//let age = 13;不能重复声明
console.log(age);
let btns = document.getElementsByTagName('button');
for(let i = 0;i<btns.length;i++){
btns[i].onclick = function () {
alert(i);
}
}
</script>
作用:定义常量
特点:不能修改、其他同let关键字
<script type="text/javascript">
const sex = '男';
console.log(sex);
//sex = '女';//不能修改
console.log(sex);
</script>
作用:从对象或数组中提取数据, 并赋值给变量(多个)
<script type="text/javascript">
let obj = {name : 'zhangsan', age : 18};
//1.对象的解构赋值
let {age} = obj;
console.log(age);
let {name, age} = {name : 'zhangsan', age : 18};
console.log(name, age);
//2. 数组的解构赋值
let arr = ['abc', 23, true];
let [a, b, c, d] = arr;
console.log(a, b, c, d);
//3. 给多个形参赋值
function person({name, age}) {
console.log(name, age);
}
person1(obj);
</script>
作用:简化字符串拼接
<script type="text/javascript">
let obj = {
name : 'zhangsan',
age : 18
};
console.log(`我叫:${obj.name}, 我的年龄是:${obj.age}`);
</script>
特点:省略同名的属性值、省略方法的function
<script type="text/javascript">
let x = 1;
let y = 2;
//简化的写法
let obj = {
x,
y,
getPoint(){
return this.x
}
};
console.log(obj, obj.getPoint());
</script>
作用:定义匿名函数。
特点:简洁、没有属于函数本身的this。
<body>
<button id="btn">测试箭头函数1</button>
<button id="btn2">测试箭头函数2</button>
</body>
<script type="application/javascript">
let func = function () {
console.log('func()');
}
func();
//1. 没有形参,并且函数体只有一条语句
let func1 = () => console.log('func1()没有形参,只有一条语句');
func1();
//2. 一个形参,函数体只有一条语句
let func2 = x => x;
console.log(func2(5));
//3. 形参是一个以上,函数体只有一条语句
let func3 = (x, y) => x + y;
console.log(func3(2, 4));
//4. 形参是一个以上,函数体有多条语句
let func4 = (x, y) => {
console.log('func4()',x, y);
return x + y;
}
console.log(func4(1,3));
//获取页面元素,没有箭头函数
let btn1 = document.getElementById('btn1');
btn1.onclick = function () {
console.log(this);
}
//获取页面元素,使用箭头函数
let btn2 = document.getElementById('btn2');
let obj = {
name : 'zhangsan',
age:39,
getName:() => {
btn2.onclick = () =>{
console.log(this);
}
}
}
obj.getName();
function Person() {
this.obj = {
showThis : () => {
console.log(this);
}
}
}
let func5 = new Person();
func5.obj.showThis();
</script>
作用:可变参数。
<script type="text/javascript">
function fun(...values) {
console.log(values);
values.forEach(function (item, index) {
console.log(item, index);
})
}
fun(1,2,3);
let arr = [2,3,4,5,6];
let arr1 = [1,...arr, 7,8,9];
console.log(arr1);
</script>
作用:调用函数时,当不传入参数的时候默认使用形参里的默认值。
<script type="text/javascript">
//定义一个点的坐标
function Point(x=0, y=0) {
this.x = x;
this.y = y;
}
let p = new Point();
console.log(p);
let point = new Point(5, 10);
console.log(point);
</script>
特点:
<script type="text/javascript">
window.onload = function () {
let symbol = Symbol();
console.log(typeof symbol);
console.log(symbol);
// 用作对象的属性(唯一)
let obj = {name: 'zhangsan', age: 18};
obj[symbol] = 'hello';
obj[symbol] = 'symbol';
console.log(obj);
for(let i in obj){
console.log(i);
}
}
</script>
概念: iterator是一种接口机制,为各种不同的数据结构提供统一的访问机制
作用:
<script type="text/javascript">
// 数组
let arr = [1, 2, 'zhangsan', true];
for(let i of arr){
console.log(i);
}
// 字符串
let str = 'aaa';
for(let item of str){
console.log(item);
}
</script>
<script type="text/javascript">
// 小试牛刀
function* generatorTest() {
console.log('函数开始执行');
yield 'hello';
console.log('函数暂停后再次启动');
yield 'generator';
}
// 生成遍历器对象
let Gt = generatorTest();
// 执行函数,遇到yield后即暂停
console.log(Gt); // 遍历器对象
let result = Gt.next(); // 函数执行,遇到yield暂停
console.log(result); // {value: "hello", done: false}
result = Gt.next(); // 函数再次启动
console.log(result); // {value: 'generator', done: false}
result = Gt.next();
console.log(result); // {value: undefined, done: true}表示函数内部状态已经遍历完毕
// 对象的Symbol.iterator属性;
let myIterable = {};
myIterable[Symbol.iterator] = function* () {
yield 1;
yield 2;
yield 4;
};
for(let i of myIterable){
console.log(i);
}
let obj = [...myIterable];
console.log(obj);
</script>
<script type="text/javascript">
class Person {
//调用类的构造方法
constructor(name, age){
this.name = name;
this.age = age;
}
//定义一般的方法
showName(){
console.log(this.name, this.age);
}
}
let person = new Person('zhangsan', 18);
console.log(person, person.showName());
//定义一个子类
class StrPerson extends Person{
constructor(name, age, salary){
super(name, age);//调用父类的构造方法
this.salary = salary;
}
showName(){//在子类自身定义方法
console.log(this.name, this.age, this.salary);
}
}
let str = new StrPerson('lisi', 18, 3000);
console.log(str);
str.showName();
</script>
特点
export 命令可以出现在模块的任何位置,但必需处于模块顶层。
import 命令会提升到整个模块的头部,首先执行。
<!-- export -->
<script>
//导出一个
export var m = 1;
// 等价于
var m = 1;
export {m}
// 导出多个
var a = 1;
var b = 2;
var c = 3;
export {a, b, c}
// 导出对象
export const student = {
name: 'Megan',
age: 18
}
// 导出函数
export function add(a, b) {
return a + b;
}
</script>
<!-- import -->
<script>
import variable from './Demo';
console.log(variable);
</script>
<script>
let promise = new Promise((resolve, reject) => {
//初始化promise状态为 pending
//执行异步操作
if(异步操作成功) {
resolve(value);//修改promise的状态为fullfilled
} else {
reject(errMsg);//修改promise的状态为rejected
}
})
</script>
<script>
promise.then(function(
result => console.log(result),
errorMsg => alert(errorMsg)
))
</script>
概念: 真正意义上去解决异步回调的问题,同步流程表达异步操作。
本质: Generator的语法糖
特点:
语法:
async function foo(){
await 异步操作;
await 异步操作;
}
<script type="text/javascript">
let str = 'abcdefg';
console.log(str.includes('a'));//true
console.log(str.includes('h'));//false
//startsWith(str) : 判断是否以指定字符串开头
console.log(str.startsWith('a'));//true
console.log(str.startsWith('d'));//false
//endsWith(str) : 判断是否以指定字符串结尾
console.log(str.endsWith('g'));//true
console.log(str.endsWith('d'));//false
//repeat(count) : 重复指定次数a
console.log(str.repeat(5));
</script>
<script type="text/javascript">
console.log(0b1010);//10
console.log(0o56);//46
//Number.isFinite(i) : 判断是否是有限大的数
console.log(Number.isFinite(NaN));//false
console.log(Number.isFinite(5));//true
//Number.isNaN(i) : 判断是否是NaN
console.log(Number.isNaN(NaN));//true
console.log(Number.isNaN(5));//falsse
//Number.isInteger(i) : 判断是否是整数
console.log(Number.isInteger(5.23));//false
console.log(Number.isInteger(5.0));//true
console.log(Number.isInteger(5));//true
//Number.parseInt(str) : 将字符串转换为对应的数值
console.log(Number.parseInt('123abc'));//123
console.log(Number.parseInt('a123abc'));//NaN
// Math.trunc(i) : 直接去除小数部分
console.log(Math.trunc(13.123));//13
</script>
<body>
<button>测试1</button>
<button>测试2</button>
<button>测试3</button>
</body>
<script type="text/javascript">
//Array.from(v) : 将伪数组对象或可遍历对象转换为真数组
let btns = document.getElementsByTagName('button');
console.log(btns.length);//3
Array.from(btns).forEach(function (item, index) {
console.log(item, index);
});
//Array.of(v1, v2, v3) : 将一系列值转换成数组
let arr = Array.of(1, 'abc', true);
console.log(arr);
//find(function(value, index, arr){return true}) : 找出第一个满足条件返回true的元素
let arr1 = [1,3,5,2,6,7,3];
let result = arr1.find(function (item, index) {
return item >3
});
console.log(result);//5
//findIndex(function(value, index, arr){return true}) : 找出第一个满足条件返回true的元素下标
let result1 = arr1.findIndex(function (item, index) {
return item >3
});
console.log(result1);//2
</script>
<script type="text/javascript">
console.log(Object.is('abc', 'abc'));//true
console.log(NaN == NaN);//false
console.log(Object.is(NaN, NaN));//true
console.log(0 == -0);//true
console.log(Object.is(0, -0));//false
//Object.assign(target, source1, source2..)
let obj = {name : 'kobe', age : 39, c: {d: 2}};
let obj1 = {};
Object.assign(obj1, obj);
console.log(obj1, obj1.name);
//直接操作 __proto__ 属性
let obj3 = {name : 'anverson', age : 41};
let obj4 = {};
obj4.__proto__ = obj3;
console.log(obj4, obj4.name, obj4.age);
</script>
<script type="text/javascript">
let set = new Set([1,2,3,4,3,2,1,6]);
console.log(set);
set.add('abc');
console.log(set, set.size);
//delete(value)
set.delete(2);
console.log(set);
//has(value)
console.log(set.has(2));//false
console.log(set.has(1));//true
//clear()
set.clear();
console.log(set);
let map = new Map([['abc', 12],[25, 'age']]);
console.log(map);
map.set('男', '性别');
console.log(map);
console.log(map.get(25));//age
//delete(key)
map.delete('男');
console.log(map);
console.log(map.has('男'));//false
console.log(map.has('abc'));//true
map.clear();
console.log(map);
</script>
for(let value of target){}循环遍历
<button>按钮1</button>
<button>按钮2</button>
<button>按钮3</button>
<script type="text/javascript">
let arr = [1,2,3,4,5];
for(let num of arr){
console.log(num);
}
let set = new Set([1,2,3,4,5]);
for(let num of set){
console.log(num);
}
let str = 'abcdefg';
for(let num of str){
console.log(num);
}
let btns = document.getElementsByTagName('button');
for(let btn of btns){
console.log(btn.innerHTML);
}
</script>