js 方法的返回值 return 用法探究

大致可以分为2种情况

第一种: 有返回值

(1) 返回true

const testFn = ()=>{

return true

}

const result = testFn()

console.log(result) 控制台打印结果为true我们可以声明一个变量去接收 进行后续操作,函数执行有结果

const testFn = ()=>{

return false

}

(2) 返回false

const result = testFn()

console.log(result)控制台打印结果为false 一般用于阻止函数默认行为;

(3) 返回方法fn

const hello =(_name)=> {

return () => {

this.todu(_name);

}

}

也可以返回一个方法 这种一般用来解决闭包问题

(4) 返回 array 或 object 

const testFn = ()=>{

return ["1","2","3"]

}

const result = testFn()

console.log(result())

const testFn = ()=>{

return {

a:1,

b:2,

c:3

}

}

const result = testFn()

console.log(result)

第二种: 没有返回值 直接return

const testFn = ()=>{

console.log(1)

return

console.log(2)

}

testFn() 

执行此方法后 控制台只输出1 没有输出2 是因为 retrun 阻止方法内部继续执行 直接跳出方法

以上仅个人发文,全凭爱好,不喜勿喷

  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值