javascript でスネーク ゲームを作成し、window.onload 関数内でオブジェクトとゲームループを作成しています。
window.onload = function() { ... Code ... };
今、関数スコープ内で作成しているオブジェクトが効率的に使用されているかどうか疑問に思っていますか? これら 2 種類の宣言の使用の違いは何ですか?
1:
var Snake = {
x: null,
y: null,
initialize: function(x, y) { this.x = x; this.y = y },
position: [x, y],
move: function(x, y) { ... Code ... }
}
2:
function Snake(x, y) {
this.x = x;
this.y = y;
this.position = function() { return [this.x, this.y]; };
this.move = function(x, y) { ... Code ... };
}
私は現在 1: ケースを使用しており、window.onload
関数のスコープからオブジェクトを呼び出しています。たとえば、次のようになります。
Snake.initialize(x, y);
while(some condition) {
Snake.move(x++, y);
}
and so on...
メモリ割り当てに違いはありますか? また、パフォーマンスの問題はありますか?