名前空間内のオブジェクトにアクセスする方法。1)スクリプトファイルをインポートするhtmlがあります
1つのスクリプトはnamesapceアプリを定義し、もう1つのスクリプトはそれにオブジェクトを添付します
オブジェクトにアクセスして使用を開始するにはどうすればよいですか
app.js
var app = (function(jQuery){
if (!jQuery) {
alert(app.resources["MISSING_LIB"]);
return null;
}
return {
init: function() {
//code for init the app related variables and subnamspaces
}
};
})(jQuery);
jQuery(document).ready(function(){
app.init();
});
personal.js
function(app){
if (app) {
app.Cache = function(nativeStorage,serializer ) {
this.storage = nativeStorage;
this.serializer = serializer;
}
app.Cache.prototype = {
setItem: function( key, value ){
this.storage.setItem(key, this.serializer.stringify( value ));
return( this );
}
};
var personalcontentstorage = new app.Cache(localStorage,JSON);
} else {
alert("app is undefined!");
}
})(app);
myhtml.html
<html>
<head>
<script src="${ '/js/app.js'" type="text/javascript"></script>
<script src="${ '/js/personal.js'" type="text/javascript"></script>
</head>
<body>
<h1>
Exploring HTML5's localStorage
</h1>
<script type="text/javascript">
jQuery(document).ready(function(){
personalcontentstorage.setItem("mykey","myValue") ;
});
</script>
</body>
</html>
オブジェクト「personalcontentstorage」にアクセスしてそのメソッドを使用できるようにする方法。オブジェクトにアクセスすると未定義のエラーが発生します。上記の方法でオブジェクトにアクセスできません。