我正在尝试在学习 Backbone 的同时构建一个简单的 2D 游戏,但我在继承的工作方式上遇到了困难。为什么没有设置 EntityModel 中的默认值?tick() 的输出始终是:
x: undefined
y: undefined
简化代码:
$(function(){
EntityModel = Backbone.Model.extend({
    defaults: function(){
        return {
            x : 0,
            y : 0
        };
    },
    tick: function(){
        console.log('x: ' + this.get('x'));
        console.log('y: ' + this.get('y'));
    }
});
PlayerModel = EntityModel.extend({
    defaults: function() {
        return {
            name : 'John Doe',
            health : 10
        };
    },
    initialize: function(options){
        console.log('New player ('+this.get('name')+') entered the game');
    }
});
var player1 = new PlayerModel();
var gameloop = window.setInterval(function(){
    player1.tick();
}, 40);
});