代码规范

2021-01-05  本文已影响0人  泡杯感冒灵

团队开发中,遵循一定的代码规范,有利于提升团队开发效率和方便后期维护。常见的代码规范 例如 airbnb规范等等。下边是一些开发我开发中尽量遵守的规范。

const obj = {}
const arr = []
const obj = {
    // 简写
    fn1(){
        console.log('fn1')
    },
    // 非简写
    fn2:function(){
      console.log('fn2')
    }
}
const userName = 'john'
const person = {
    // 相当于 userName:userName 
    userName,
    // 最后一个属性后边尽量跟一个逗号
    age:20,
}
// 例如我们在调用云函数的时候,如果我们直接用普通的函数模式作为then的回调。那么这个时候会报错。`cannot read property setData of undefined`。因为此时this指向不对
wx.cloud.callFunction({
    name:'login'
}).then(function(res){
    console.log(res)
    this.setData({
        openid:res.result.openid
    })
})

// 除非我们先获取到this。以前通常的做法是 
const that= this
wx.cloud.callFunction({
    name:'login'
}).then(function(res){
    console.log(res)
    that.setData({
        openid:res.result.openid
    })
})

// 现在我们直接用箭头函数会方便很多
wx.cloud.callFunction({
    name:'login'
}).then((res) => {
    console.log(res)
    that.setData({
        openid:res.result.openid
    })
})
当然还有更多的规范可以应用,在这里就不做举例了。以后有时间可以慢慢增加
上一篇 下一篇

猜你喜欢

热点阅读