trans( cb => { //this 是 node-mysql 里面的 connection 对象(可以理解成原生的吧) this.query("select * from xxx", cb); }, cb => { this.query("update xxxx", cb); }, (err, r) => { console.log(r[0], r[1]); } );
上面把 beginTransaction ,rollback,commit 都封装了,所以过程使用了同一个数据库连接
回复 (3)
2
遇到一个宇宙级难题
var a = cb => { console.log(this); } 这种 es6 语法,居然把 this 给干掉了,也就是在此函数体里 this 永远都是 {}
var b = function (cb) { console.log(this); } 这种原始语法就不会有这种问题
a.apply({xx:100}, []); b.apply({xx:100}, []);
X
箭头函数的this本来就是它的亮点之一。
参与回复
登录后即可参与回复。登录