スニペット 1 をスニペット 2 にリファクタリングしたいと思います。サイズを考えるとパフォーマンスはそれほど問題ではないと思いますが、モジュール パターンへのこのリファクタリングに関して、メモリ使用量に関して何が起こっているのかを理解したかったのです。
モジュール パターンにより、このデータを DOM から 1 回だけプルすることが保証されます。これは私が望むものであり、データが非公開であるという点でミニ レジストリ パターンも形成します。
どちらのスニペットもテスト済みで、基本的に機能します。
スニペット 1 // SUniverisals を SU に置き換えます
var SUniversals = function () {
// Pull from Server
this.universals.path = document.getElementById('universals').getAttribute('data-path');
this.universals.load = document.getElementById('universals').getAttribute('data-load');
// Set Manually
this.universals.debug = false;
};
SUniversals.prototype.universals = {};
SUniversals.prototype.get = function( key ) {
return this.universals[ key ];
};
SUniversals.prototype.set = function( key, value ) {
this.universals[ key ] = value;
};
スニペット 2
var SU = ( function ()
{
// private SU.get('load');
var universals = {};
universals.path = document.getElementById('universals').getAttribute('data-path');
universals.load = document.getElementById('universals').getAttribute('data-load');
universals.debug = false;
// pubulic
var publik = {};
publik.get = function( key )
{
return universals[ key ];
};
publik.set = function( key, value )
{
universals[ key ] = value;
};
return publik;
}());