const 变量不变 let 变量可变
箭头函数,支持默认设置参数
const fn=(a=1,b=2)=>{return a+b;}
const fn1 = (a,b)=>a+b; //简写,当返回值可省略括号
let fn=new Promise(function(resolve,reject){
resolve("输出信息")
})
fn.then(function (rs){
console.log(rs)
})
字符模板,不再使用+链接字符串
const a = 20;
const b = 30;
const string = `${a}+${b}=${a+b}`;
解析结构
const obj={
a:1,
b:2,
c:3
}
const {a,b}=obj; 对象解析结构
let [a, b, c] = [1, 2, 3];数组的解析结构
let [a, ...b] = [1, 2, 3]; 剩余运算 a=1 b=[2,3]
简写,当属性和值相同
const person = { name, age,fn(){
return this.name;
} }等价
var person = {
name: name,
age: age
,
fn:function fn(){
return this.name;
}
};
类
class Person {
//构造方法
constructor(name, age) { this.name = name; this.age = age; }
getName() { return this.name }
}
模块
接口定义可以是变量,函数,类
var m=1
export fn
export default默认方法或变量,只能一个,不能用花括号
import {m} from ‘m’;
ajax请求
fetch(“/hi”,{method:”GET”})
.then(res=>res.json())
.then(res=>{
console.log(res.name)
})
.catch(e=>alert(‘出错了’+e))
post标准提交
fetch(“/hi”,{method:”POST”,body: ‘name=中国’,mode:”cors”,headers: {
“Content-type”: “application/x-www-form-urlencoded; charset=UTF-8”,
},})
.then(res=>res.json())
.then(res=>{
console.log(res.name)
})
.catch(e=>alert(‘出错了’+e))
//post json提交 默认
fetch(“/hi”,{method:”POST”,body: JSON.stringify({“name”:”中国”}),mode:”cors”,headers: {
//“Content-type”:”application/json;charset=utf-8”,
},})
.then(res=>res.json())
.then(res=>{
console.log(res.name)
})
.catch(e=>alert(‘出错了’+e))
更新时间:2024-12-05 13:26