Page({
data:{
autoIncrementId:null,
directionStartBrake:null,
motor:{
pwm:0,
}
},
directionTap:function(){
let _this = this
console.log("tap")
console.log(_this.data.motor)
_this.directionExec()
},
directionStart:function(){
let _this = this
console.log("start")
console.log(_this.data.motor)
},
directionLongpress:function() {
const _this = this;
console.log("long")
console.log(_this.data.motor)
const autoIncrementId = setInterval(() => {
_this.directionExec()
}, 500);
this.data.autoIncrementId = autoIncrementId;
},
directionStop:function(){
let _this = this
console.log("stop")
console.log(_this.data.motor)
if (this.data.autoIncrementId == null) return;
clearInterval(this.data.autoIncrementId);
this.data.autoIncrementId = null;
},
directionExec:function(){
const count = _this.data.motor.pwm+=10;
_this.setData({
"motor.pwm":count
});
},
})
我发现所有的console.log(_this.data.motor)结果都是const count = _this.data.motor.pwm+=10;之后的,我想实现,tap开始的时候先记录_this.data.motor.pwm的初始值,在directionStop的时候再放回去,为什么,所有的值都是相同的,好像是传址了,请大师帮忙看看,谢谢
console.log 的对象是引用关系,要打印快照可以用个简陋的深拷贝
可以用一个变量接着 const value = _this.data.motor console.log(value)