JS design mode (Factory mode environment construction)

Source: Internet
Author: User

<!--Introducing the core JS file--
<script type= "Text/javascript" src= "Commonutil.js" ></script>
<script type=text/javascript charset=utf-8>
<!--JS design mode (Factory mode)--
//Build a factory environment
//Car Shop
function Carshop () {};
carshop.prototype={
Constructor:carshop,
sellcar:function (type) {//Sell vehicle Method
var car;//declares a variable
switch (type) {
Case ' Benz ': car=new Benz ();
Case ' BMW ': Car=new BMW ();
Case ' Audi ': Car=new Audi ();
DEFANLT: ' Not buy it ';
}
//test Interface Implementation
Jalja. Interface.ensureimplements (car,carinterface);
return car;
}
};
var carinterface =new Jalja. Interface ("Carinterface", [' Start ', ' Run ']);
//Parent class of all vehicles
function Basecar () {};
basecar.prototype={
Constructor:basecar,
start:function () {
Alert (This.constructor.name + ": Start");
},
run:function () {
Alert (this.constructor.name + ": Run");
}
}
//Specific car
function Benz () {};
//Inheritance--put in front of unique attributes
jalja.extend (benz,basecar);
//define your own unique properties
benz.prototype.driverbenz=function () {
alert ("Benz unique 1");
};
function Bmw () {};
//Inheritance--put in front of unique attributes
jalja.extend (bmw,basecar);
//define your own unique properties
bmw.prototype.driverbmw=function () {
alert ("BMW unique 1");
};
function Audi () {};
//Inheritance--put in front of unique attributes
jalja.extend (audi,basecar);
//define your own unique properties
audi.prototype.driveraudi=function () {
alert ("Audi-specific 1");
};
//Test
var shop =new carshop ();
var car1=shop.sellcar (' Audi ');
Car1.run ();
Car1.start ();
Car1.driveraudi ();
var car2=shop.sellcar (' Benz ');
Car2.run ();
Car2.start ();
Car2.driverbenz ();
</script>

JS design mode (Factory mode environment Setup)

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.