<!--———————— js design mode monomer mode ———————-->
//1. Simple monomer Mode
var singleton={
Attr1:true,
Attr2:10,
method1:function () {
alert ("Method One");
},
method2:function () {
alert ("Method two");
}
};
//Monomer mode partition namespace (differentiate code)
var xl={};//create your own namespace
XL. singleton2={
Attr1:true,
Attr2:10,
method1:function () {
alert ("Method One");
},
method2:function () {
alert ("Method two");
}
};
XL. Singleton2.method1 ();
//2. Using closures to create a single closure: used primarily to protect data from external interference
XL. singleton3= (function () {
//Add your own member variable
var name= "Zhang San";
var f=function () {
alert ("F");
};
//Assign the execution result of the block-level scope to the monomer object
return {
Arrt3:name,
method1:function () {
alert ("Method One");
},
method2:function () {
alert ("Method two");
},
method3:function () {
return F ();
}
};
})();
alert (XL. SINGLETON3.ARRT3);
XL. Singleton3.method3 ();
//3. Inert monomers
XL. Base= (function () {
var uniq;//uses private variables to control return monomers
function init () {
//Private member variable
var a1=10;
var a2=true;
var fn1=function () {alert ("fn1");};
var fn2=function () {alert ("fn2");};
return {
ATTR3:A1,
Attr4:a2,
method4:function () {
return fn1 ();
},
method5:function () {
return fn2 ();
}
};
}
return {
gentinstance:function () {
if (!uniq) {//If the singleton instance does not exist, create an instance monomer
uniq=init ();
}
return uniq;
}
}
})();
XL. Base.gentinstance (). METHOD5 ();
//4. Branch of branch monomer judgment program for browser diff detection
var def=true;
XL. More= (function () {
var objecta={//firefox browser
FIX: "FF"
};
var objectb={//ie browser
FIX: "Ie"
}
Return (def)? OBJECTA:OBJECTB;
})();
Alert (XL. More.fix);
JS monomer Mode