JS monomer Mode

Source: Internet
Author: User

<!--———————— 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.