<script>
// js 中的多个连续的箭头函数与柯里化
let add1 = x => y => {
return x + y;
}
let add2 = (x, y) => {
return x + y;
}
// 两种结果相同
console.log(add1()) // 结果为 y => {return x + y;}
console.log(add1(1)(2)) // 返回结果
console.log(add2(1, 2))
// json.stringfy()
const useInfo = {
usercode: "this.loginForm.usercode",
password: "this.loginForm.password",
comcode: "this.loginForm.comcode"
}
console.log(JSON.stringify(useInfo)) // {"usercode":"this.loginForm.usercode","password":"this.loginForm.password","comcode":"this.loginForm.comcode"}
// 深拷贝
console.log(JSON.parse(JSON.stringify(useInfo))) // {usercode: "this.loginForm.usercode", password: "this.loginForm.password", comcode: "this.loginForm.comcode"}
// 数组、对象遍历
var arra = [
{
a1: 'a11'
},
{
b1: 'b11'
},
{
c1: 'c11'
},
{
d1: 'd11'
},
{
e1: 'e11'
}
]
var arrb = [
{
b1: 'b11',
b2: 'b21',
b3: 'b31',
b4: 'b41',
b5: 'b51',
b6: 'b61'
}
]
// 数组中多个对象
arra.forEach(item => {
if (item.a1 === 'a11') return (console.log(item.a1))
})
// 数组中多个对象 使用对应下标获取对应对象
console.log(arra[0].a1)
// 数组中一个对象 可以使用下标
console.log(arrb[0].b1)
var asd = 'aaa'
var bnm = 'bbb'
// ES6 模板语法
console.log({lable: `${asd}-${bnm}`})
function promiseText(a) {
// New 一个 Promise 对象,并返回
return new Promise((resolve, reject) => {
const sino = parseInt(Math.random() * 6 + 1)
if (sino > 3) {
if (a === '大') {
resolve(true)
} else {
reject('失败'+false)
}
} else {
if (a === '大') {
reject('失败'+false)
} else {
resolve(true)
}
}
})
}
// 异步函数 async 使用await 接受Promise对象返回的结果 (异步: 无结果不执行后续代码 console.log(1111111) )
async function text(a) {
try {
let rel = await promiseText(a)
console.log(`a:${rel}`)
console.log(1111111)
} catch (err) {
console.log(err)
}
}
// 使用 try catch 的方式获取 返回的 Promise 对象 捕获异常
text('大')
function promiseText1(a) {
// 返回一个Promise 对象 结果使用 resolve
return new Promise(resolve => {
const sino = parseInt(Math.random() * 6 + 1)
if (sino > 3) {
if (a === '大') {
resolve(true)
} else {
resolve('失败'+false)
}
} else {
if (a === '大') {
resolve('失败'+false)
} else {
resolve(true)
}
}
})
}
// 可以不适用try catch 捕获异常 (异常使用reject 返回,此处没有reject)
async function text1(a) {
let rel = await promiseText1(a)
console.log(rel)
}
text1('大')
function promiseText2(a) {
return new Promise((resolve, reject) => {
const sino = parseInt(Math.random() * 6 + 1)
if (sino > 3) {
if (a === '大') {
resolve(true)
} else {
reject(false)
}
} else {
if (a === '大') {
reject(false)
} else {
resolve(true)
}
}
})
}
// 使用 .then .catch 的方式接收返回的 Promise 对象
promiseText2('大').then(rel=>{console.log(rel)}).catch(err=>{console.log(err)})
</script>
数组遍历
<script>
var arr = [1, 2, 3]
function arrfun() {
arr.forEach((item, i) => {
console.log('forEach 遍历 使用return无法阻断当前函数执行后续代码、不能返回到外层函数。 break不能中断循环,一个参数时遍历结果为数组内容两个为下表和数组内容')
return console.log(`1:${item}:${i}`)
})
for (var item in arr) {
console.log('for in 遍历 使用return 可以阻断当前函数执行后续代码,且遍历结果为数组下标')
return console.log(`2:${item}`)
}
for (var i = 0; i <= arr.length; i++) {
console.log('for 循环使用return 可以阻断当前函数执行后续代码,且遍历结果为数组下标')
return console.log(`3;${arr[i]}`)
}
for (var item of arr) {
console.log('for of 遍历的是数组的结果,使用return可以阻断当前函数执行后续代码')
return console.log(`4:${item}`)
}
}
arrfun()
</script>