我正在探索开源开发,并使用自耕农和淘汰发电机与十字路口和requirejs。我已经查看了ES6类和构造函数,并且在某些情况下我不能完全理解敲除的情况,在这种情况下,可观察的是ES6类或函数模式中没有发生函数错误。所以我之前已经看过淘汰赛的这种行为,但如果可以,我会想要一些额外的细节。
此行为的编写码:http://codepen.io/srabeeh/pen/pjqweX?editors=001
有人可以解释失败的函数查找的来源是什么吗? 代码:
class Circle {
constructor(diameter, quality) {
this.diameter = ko.observable(diameter);
this.quality = ko.observable(quality);
}
updateCircle(){
console.log('updating...');
// the correct way to update an observable
// this line fails with diameter is not a function - uncomment to see error
// this.diameter(this.diameter() +1);
// this works but i fear this obliterates the observable
this.diameter += 1;
console.log(this.diameter); // gives NaN of course as it's an observable
//function call fails that it's not a function
console.log(this.diameter());
}
startTimer(){
var timer = setInterval(this.updateCircle, 1000)
}
};
let c = new Circle(270, 5);
c.startTimer();
由于
答案 0 :(得分:0)
这个指针确实存在问题。试试这个:
class Circle {
constructor(diameter, quality) {
this.diameter = ko.observable(diameter);
this.quality = ko.observable(quality);
}
updateCircle() {
console.log('updating...');
this.diameter(this.diameter() + 1);
console.log(this.diameter());
}
startTimer() {
var timer = setInterval(function() {
this.updateCircle()
}.bind(this), 1000)
}
};
let c = new Circle(270, 5);
c.startTimer();
console.log("started")
注意绑定。