三、JavaScript函数[四种函数格式、this]

一、函数

这里不详细介绍函数,因为了解过其它语言,下面主要说的是JavaScript函数的独特之处

1.普通函数

function hello(num) {
	return num+10;
}

2.对象属性函数

    let hello = {
        name: 15,
        Whoyour: function (name) {
            return name+'ok';
        },
        //简写
        Noyour(value) {
            this.name = value;
        }
    }
    console.log(hello.name); // 15
    console.log(hello.Whoyour('15551'))//15551ok

3.匿名函数

let hello = function(num) {
  return num+20;
};
console.log(hello(15))// 35

4.箭头函数

解释:推荐在把函数用作参数时使用

1
setTimeout(()=>{
    console.log(123)
},1000)2
// 其中a和b都是参数
let doo= (a,b)=>{
    console.log(a,b)
}
// 只有一个参数可以不加括号
let Hello = a=>{
    console.log(a)
}
Hello(55)//55
doo(15,20)//15 20

二、this

关于this的指向问题

1.普通函数

解释:此时的this指向的winodw,在vue中此时this指向的是vue

let Hello = a=>{
    console.log(this)
}
Hello()//Window {window: Window, self: Window, document: document, name: '', location: Location, …}

2.对象属性函数

let hello = {
    a:55,
    b:function(){
        console.log(this)
    }
}
hello.b()// {a: 55, b: ƒ}

3.箭头函数

解释:箭头函数没有this, 也可以理解为箭头函数中的this 会继承定义函数时的上下文,可以理解为和外层函数指向同一个 this

let hello = {
    a:55,
    b:()=>{ //使用箭头函数
        console.log(this)
    }
}
hello.b()//Window {window: Window, self: Window, document: document, name: '', location: Location, …}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值