(function(window,namespace,undefined){
if(!window[namespace]){
window[namespace] = G;
}else{
throw new Error("this namespace [" + namespace + "] is already defined.");
}
function G(a){
this.message = a || "write somthing";
}
G.prototype.show = function(){
alert(this.message);
};
G.prototype.unko = function(){
alert(this.message + ' unko');
};
}(this,'GAINA'));
var a = new GAINA();
var b = new GAINA("unkokoko");
a.show(); //write somthing
b.show(); //unkokoko
みたいのではダメなのかなぁ…
namespaceも使えるやつだけぶっこんで確かめたりするのかなぁ…
うーん…
if(!namespaceCreate){
var namespaceCreate = {};
}
function namespaceCreate(global,namespace,classname){
this.global = global;
this.namespace = namespace;
this.classname = classname;
this.ns = "";
}
namespaceCreate.prototype.create = function(){
if(this.namespace instanceof Array){
var len = this.namespace.length,i;
for(i = 0;i < len; i +=1){
if(!this.global[this.namespace[i]]){
this.global[this.namespace[i]] = this.classname;
this.ns = this.namespace[i];
return;
}
if(i === len - 1){
throw new Error('these namespace is already defined.');
}
}
}else{
if(!this.global[this.namespace]){
this.global[this.namespace] = this.classname;
this.ns = this.namespace;
}else{
throw new Error('this namespace [' + this.namespace + '] is already defined.');
}
}
};
namespaceCreate.prototype.debug = function(){
if(window.console){
console.log('使用するnamespaceは [' + this.ns + '] です');
}else{
alert('使用するnamespaceは [' + this.ns + '] です');
}
};
(function(window,namespace,undefined){
var ns = new namespaceCreate(window,namespace,G);
ns.create();
ns.debug();
//namespaceCreate(window,namespace,G);
function G(a){
this.message = a || "write somthing";
}
G.prototype.show = function(){
alert(this.message);
};
G.prototype.unko = function(){
alert(this.message + ' unko');
};
}(this,['location','window','GAINA']));
var a = new GAINA();
var b = new GAINA("unkokoko");
a.show(); // write somthing
b.show(); // unkokoko
そもそもnamespaceってこんな認識であってるのかもわかんないわー
うだうだやって、最終型
/* ---------------
*
* 名前空間生成
* @class グローバルに色々ぶっこみたいけど名前空間汚しなくないクラス
* @param {window} global
* @param {String} namespace
*
--------------- */
if(!GNamespace){
var GNamespace = {};
}
function GNamespace(global,namespace){
if(typeof(namespace) !== 'string'){
throw new Error();
}
var global = global,
namespace = namespace || 'GNameSpaceObject',
func = [],
classname = [],
ns = "",
container = global,
count = 0;
this.namespace = namespace;
return {
/**
* 名前空間に追加したい関数をぶっこむ
* @param {Function} funct 関数でもクラスもどきでも
* @return this 本体
*/
add:function(funct){
func.push(funct);
// 名前は関数を文字列化してゴニョゴニョして取得
classname.push(funct.toString().replace(/(function\s)(\w*)(\([\w\s\(\)\{\}\[\]\.\-\_\@\|\&\$\*\=\"\'\\\;\?]*)/,'$2'));
count += 1;
return this;
},
/**
* ぶっこまれた関数たちを名前空間に登録する
* @return this 本体
*/
register:function(){
// "gaina-laboratory"のように[-]繋ぎで一単語の場合、
// 別オブジェクト(gainaとlaboratory)にされちゃうのでアンダーバーに変換する
namespace = namespace.replace(/-/,'_');
// "com.gaina"のような[.]連結の場合
if(namespace.split('.').length > 1){
var n,names = namespace.split('.');
container = global;
for(n = 0; n < names.length; n += 1){
if(typeof(container[names[n]]) === 'undefined'){
container[names[n]] = {};
}
container = container[names[n]];
}
var m;
for(m = 0;m<count;m +=1){
container[classname[m]] = func[m];
}
// 一単語の場合
}else{
if(typeof(global[namespace]) === 'undefined'){
global[namespace] = {};
}
var m;
for(m = 0;m<count;m +=1){
global[namespace][classname[m]] = func[m];
}
}
return this;
},
/**
* 名前空間をコンソールに表示させる
*/
debug : function(){
if(window.console){
console.log('---- namespace ----');
console.log(namespace);
}
}
};
}
(function(window,namespace,undefined){
var ns = new GNamespace(window,namespace).add(GA).add(G).register().debug();
function GA(){
this.unko = 'morimori';
}
GA.prototype.unko = function(){
alert(this.unko);
};
function G(a){
this.message = a || "write somthing";
}
G.prototype.show = function(){
alert(this.message);
};
G.prototype.unko = function(){
alert(this.message + ' unko');
};
}(this,'com.gaina-laboratory'));
(function(window,namespace,undefined){
var ns = new GNamespace(window,namespace).add(AAA).add(BBB).register().debug();
function BBB(){
this.unko = 'morimori';
}
BBB.prototype.unko = function(){
alert(this.unko);
};
function AAA(a){
this.message = a || "write somthing";
}
AAA.prototype.show = function(){
alert(this.message);
};
AAA.prototype.unko = function(){
alert(this.message + ' unko');
};
}(this,'com.gaina-laboratory.unko'));
console.log(com);