我定義我的課像這樣:JavaScript類方法
function Slot(slot, maxSpeed, timer) {
this.slot = slot;
this.speed = 0;
this.maxSpeed = maxSpeed;
this.timer = timer;
this.interval = null;
this.go = function() {
var $slot = $(this.slot);
console.log(this.slot);
$slot.addClass('motion');
$slot.spStart();
this.interval = window.setInterval(function() {
var step = Math.floor(Math.random() * ((this.speed/100) * 25)) + 1;
if (this.speed < (this.maxSpeed/2)) {
this.speed += step;
}
if (this.speed >= (this.maxSpeed/2)) {
this.speed -= step;
}
console.log(this.slot);
$slot.spSpeed(this.speed);
}, timer);
};
$(this.slot).pan({ fps: '30', dir: 'down' });
$(this.slot).spStop();
}
第一的console.log返回預期值,但一旦我進入了setInterval函數的所有變量(this.slot,this.speed)都未定義?儘管我仍然在他們的範圍內......