链式操作的核心原理
test.js
var c = require('./chained1');
// c.select();
c.where('id=2').select();
chained.js
module.exports = {
where: function (wh) {
this.whe = wh;
// 链式操作的核心就是保存数据并返回本对象
return this;
},
select: function () {
if (this.whe == undefined) {
var sql = "select * from users ";
} else {
var sql = "select * from users where " + this.whe;
}
console.log(sql);
this.whe = undefined;
},
update:function(){
if (this.whe == undefined) {
console.log('更新数据时,请填写where条件');
return;
} else {
var sql = "update xx from set () where " + this.whe;
}
console.log(sql);
this.whe = undefined;
}
}