我正在尝试创建一个无穷无尽的原型函数。这是我的尝试,但不起作用:
function Cat(name, direction) {
this.name = name;
this.stand = {left: "images/standLeft.png", right: "images/standRight.png"};
this.walk = {left: "images/walkLeft.gif", right: "images/walkRight.gif"};
this.direction = direction;
}
Cat.prototype.walking = function() {
var myDirection = (this.direction == "right" ? "+=": "-=");
var myPosition = $("#cat").css("left");
myPosition = myPosition.substring(0, myPosition.length - 2);
var distanceLeft = myPosition;
var distanceRight = 1024 - myPosition - 173;
if(this.direction == "right") {
var distance = distanceRight;
} else {
var distance = distanceLeft;
}
$("#cat img")[0].src = this.walk[this.direction];
$("#cat").animate({
left: myDirection + distance
}, (22.85 * distance), function(){
this.direction = (this.direction == "right" ? "left": "right");
this.walking();
});
}
var myCat = new Cat("Izzy", "right");
我认为调用(this.walking())
会再次使用相同的对象运行相同的函数,但是它会引发错误。有什么想法或建议吗?