es6

1 箭头函数

箭头函数 是 函数的一种简写形式,使用括号来包裹参数,跟随一个  =>  ,紧接着是函数体。

复制代码
var add = function(x,y){
  return x + y
}

var add = (x,y) => x + y

var add = (x,y) => {
  return x + y
}
console.log(add(4,7))
复制代码

2 对象和数组结构

1
2
3
4
5
arr = ["jink",23,"shanxi"]
 
let [name,age,add] = arr
 
console.log(name,age,add) <br><br><br>

ob = {x:4,y:5,z:6}

let {x:x,y:y,z:z} = ob

console.log(x,y,z)  //输出4 , 5  , 6

 

3 字符串中渲染变量

var name = "jinkang"
var cen = `welcome ${name}`  (反分号,TAB 上面那个)
console.log(cen)  //输出 welcome jinkang

 

4 使用 for of 和 for in 遍历一个迭代器

1
2
3
4
5
6
7
8
for (let per of stus){
  console.log(per) //zs ls ww
}
 
for (let per in stus){
  console.log(per) //0 1 2
  console.log(stus[per]) //zs ls ww
}

5 Promise

1
2
3
4
5
6
7
8
9
10
let promise = new Promise(function(resolve,reject){
  console.log("promise up");
  resolve()
})
 
promise.then(function(){
  console.log("resolveed");
})
 
console.log("hi");  <br>输出: promise up<br>      hi<br>      resolveed

上面代码中,Promise 新建后立即执行,所以首先输出的是Promise up。然后,then方法指定的回调函数,将在当前脚本所有同步任务执行完才会执行,所以resolved最后输出。

 

复制代码
var request = require("request")

const options = {
    url: "http://mail.zhonghuass.cn/hello",
    method: "POST",
    headers:{
        "content-type": "text/html;charset=utf-8",
    }
}

var pro = new Promise(function(resolve,reject){
request(options,(err,res,body) => {
    if(err){
        reject("wrong")
    }
    resolve(body)
})
})

pro.then(function(value){
console.log('OK');
console.log(value)
},function(error){
console.log(error);
})
复制代码

then  方法 的 作用是为 Promise 实例添加状态改变时间时的回调函数。then 方法的第一个参数是 resolved 状态的回调函数,第二个参数(可选)是 reject 状态的回调函数。

6 Generator

复制代码
function* f() {

  yield 5;
  yield 23;
  yield 211;
  yield 12;
}

var a = f()

console.log(a.next())    
console.log(a.next())
console.log(a.next())
输出

{value:5,done:false}
{value:23,done:false}
{value:211,done:false}

for...of循环可以自动遍历 Generator 函数时生成的Iterator对象,且此时不再需要调用next方法
for (let a of f()){
    console.log(a)
}  输出//5   23   211  12 
复制代码

 

posted on   思此狂  阅读(168)  评论(0编辑  收藏  举报

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示