私は小さなゲームを開発しており、次のようないくつかのオブジェクトをインスタンス化しています:
this.polyfills = new Application.Polyfills(this.options);
問題は、クリック時に(ページ上のいくつかの要素の)何度も呼び出されるコードの一部であり、その目的をすでに果たしているため、毎回そのオブジェクトをインスタンス化したくないということです。私はこのようなことを試しました:
this.polyfills = window.Application.Polyfills || new Application.Polyfills(this.options);
しかし、上記は明らかに機能しません:)では、今説明したことを実行するにはどうすればよいでしょうか?
EDIT:クリック時にインスタンス化されるオブジェクト(私はそれをクラスと呼びます)はこれです:
Application.Level = function(_level, _levels, callback) {
this.helpers = (this.helpers instanceof Application.Helpers) ? true : new Application.Helpers();
var self = this,
_options = {
levels : {
count : _levels,
settings : (_level === undefined || _level === '') ? null : self.setLevelSettings(_level, _levels),
template : 'templates/levels.php'
},
api : {
getImages : {
service : 'api/get-images.php',
directory : 'assets/img/'
}
}
};
this.options = new Application.Defaults(_options);
this.polyfills = (this.polyfills instanceof Application.Polyfills) ? true : new Application.Polyfills(this.options);
return this.getImages(self.options.api.getImages.service, { url : self.options.api.getImages.directory }, function(data){
return (typeof callback === 'function' && callback !== undefined) ? callback.apply( callback, [ data, self.options, self.helpers ] ) : 'Argument : Invalid [ Function Required ]';
});
};
を通じて定義されたプロパティのコレクションが含まれていますprototype
。だから私がやろうとしていることは不可能かもしれません?!