两段关于模块模式的代码
记性不好,特此记录。
代码一:
var Co = (function () {
function getTypeOf(o) {
return typeof o;
}
function getPrototypeToString(o) {
return Object.prototype.toString.call(o);
}
return {
GetTypeOf: function (o) {
return getTypeOf(o);
},
GetPrototypeToString: function (o) {
return getPrototypeToString(o);
}
}
})();
Co.GetTypeOf(0.23);
Co.GetTypeOf("test");
Co.GetTypeOf({});
Co.GetTypeOf([]);
Co.GetTypeOf(/abc/gi);
Co.GetTypeOf(true);
//------------------华丽分割线--------------------
Co.GetPrototypeToString(0.23);
Co.GetPrototypeToString("test");
Co.GetPrototypeToString({});
Co.GetPrototypeToString([]);
Co.GetPrototypeToString(/abc/gi);
Co.GetPrototypeToString(true);
代码二:
//单例
var singleton = (function () {
var instance;
function init() {
function privateMethod() {
console.log("privateMethod.");
}
var privateVariable = "privateVariable.";
var privateRandomNum = Math.random();
return {
//publicMethod: privateMethod,
publicMethod: function () {
privateMethod();
},
publicProperty: "publicProperty.",
getRandomNum: function () {
return privateRandomNum;
}
}
}
return {
getInstance: function () {
if (!instance) {
instance = init();
}
return instance;
}
}
})();