先来一个解决方法希望看得懂<script type="text/javascript">
/*<![CDATA[*/function Interface(name_, set_)
{/* shawl.qiu code */
var This = this;
if(!name_) { throw new Error('Interface property name_ is null!'); };
This.name = name_;
if(!set_) { throw new Error(format_f('from Interface "{0}", property set_ is null!', name_)); };
This.set = set_;
}/* function Interface(name_, set_) */Interface.prototype.valid = interface_valid_f;function interface_valid_f(class_ins)
{
var This = this;
for(var key_ in This.set)
{
var interface_item = This.set[key_];
var class_item = class_ins[key_];
if(typeof class_item=="undefined")
{
switch(typeof interface_item)
{
case "function":
throw new Error(format_f("Interface '{0}', method '{1}' is null!", This.name, key_));
break;
default:
if(typeof class_item!="number")
{
throw new Error(format_f("Interface '{0}', property '{1}' is null!", This.name, key_));
}
break;
}
}
else if(typeof interface_item!=typeof class_item)
{
throw new Error
(
format_f
(
"Interface '{0}', property '{1}' type {2} and instance class property '{1}' type {3}, type is not the same!"
, This.name
, key_
, typeof interface_item
, typeof class_item
)
);
}
}/* for(var key_ in This.set) */
return;
}/* function interface_valid_f(class_ins) */function format_f(sStr)
{/* shawl.qiu code, return string */
var Len = arguments.length, Re = null;
switch(Len)
{
case 0: return "";
case 1: return sStr;
}
for(var i=1, j=0; i<Len; i++, j++)
{
Re = new RegExp(["\\{", j, "\\}"].join(""), "g");
sStr = sStr.replace(Re, arguments[i]);
}
return sStr;
}/* function format_f(sStr) */
var AbstractFactory_Interface =
new Interface
(
'AbstractFactory_Interface'
, {
CreateProductA:function(){}
, CreateProductB: function(){}
}
);var AbstractFactory =
{
id:"ok"
}AbstractFactory.prototype = {};
AbstractFactory.prototype.interface = AbstractFactory_Interface;function ConcreteFactory1()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
this.CreateProductA = function(){return new ProductA1();}
this.CreateProductB = function(){return new ProductB1();} if(this.interface) this.interface.valid(this);
}
ConcreteFactory1.prototype = AbstractFactory;function ConcreteFactory2()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
this.CreateProductA = function(){return new ProductA2();}
this.CreateProductB = function(){return new ProductB2();} if(this.interface) this.interface.valid(this);
}
ConcreteFactory2.prototype = AbstractFactory;
var AbstractProductB_Interface =
new Interface
(
'AbstractProductB_Interface'
, {
Interact:function(AbstractProductA_Instance){}
}
);var AbstractProductA =
{}
AbstractProductA.prototype = {};
var AbstractProductB =
{}
AbstractProductB.prototype = {};
AbstractProductB.prototype.interface = AbstractProductB_Interface;function ProductA1()
{
this.id = "ProductA1";
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
if(this.interface) this.interface.valid(this);
}
ProductA1.prototype = AbstractProductA;function ProductB1()
{
this.id = "ProductB1";
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
if(this.interface) this.interface.valid(this);
}
ProductB1.prototype = AbstractProductB;
ProductB1.prototype.Interact =
function(AbstractProductA_Instance)
{
document.write(this.id + " interacts with " + AbstractProductA_Instance.id, "<br/>");
};function ProductA2()
{
this.id = "ProductA2";
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
if(this.interface) this.interface.valid(this);
}
ProductA2.prototype = AbstractProductA;function ProductB2()
{
this.id = "ProductB2";
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
if(this.interface) this.interface.valid(this);
}
ProductB2.prototype = AbstractProductB;
ProductB2.prototype.Interact =
function(AbstractProductA_Instance)
{
document.write(this.id + " interacts with " + AbstractProductA_Instance.id, "<br/>");
};
function Environment(factory_instance)
{
var AbstractProductA_Instance = null;
var AbstractProductB_Instance = null;
AbstractProductA_Instance = factory_instance.CreateProductA();
//document.write("<h2>",AbstractProductA_Instance.id,"</h2>");
AbstractProductB_Instance = factory_instance.CreateProductB();
this.Run =
function()
{
AbstractProductB_Instance.Interact(AbstractProductA_Instance);
}
}var AbstractFactory_Instance = null;
var Environment_Instance = null;AbstractFactory_Instance = new ConcreteFactory1();
Environment_Instance = new Environment(AbstractFactory_Instance);
Environment_Instance.Run();AbstractFactory_Instance = new ConcreteFactory2();
Environment_Instance = new Environment(AbstractFactory_Instance);
Environment_Instance.Run();/*]]>*/
</script>
/*<![CDATA[*/function Interface(name_, set_)
{/* shawl.qiu code */
var This = this;
if(!name_) { throw new Error('Interface property name_ is null!'); };
This.name = name_;
if(!set_) { throw new Error(format_f('from Interface "{0}", property set_ is null!', name_)); };
This.set = set_;
}/* function Interface(name_, set_) */Interface.prototype.valid = interface_valid_f;function interface_valid_f(class_ins)
{
var This = this;
for(var key_ in This.set)
{
var interface_item = This.set[key_];
var class_item = class_ins[key_];
if(typeof class_item=="undefined")
{
switch(typeof interface_item)
{
case "function":
throw new Error(format_f("Interface '{0}', method '{1}' is null!", This.name, key_));
break;
default:
if(typeof class_item!="number")
{
throw new Error(format_f("Interface '{0}', property '{1}' is null!", This.name, key_));
}
break;
}
}
else if(typeof interface_item!=typeof class_item)
{
throw new Error
(
format_f
(
"Interface '{0}', property '{1}' type {2} and instance class property '{1}' type {3}, type is not the same!"
, This.name
, key_
, typeof interface_item
, typeof class_item
)
);
}
}/* for(var key_ in This.set) */
return;
}/* function interface_valid_f(class_ins) */function format_f(sStr)
{/* shawl.qiu code, return string */
var Len = arguments.length, Re = null;
switch(Len)
{
case 0: return "";
case 1: return sStr;
}
for(var i=1, j=0; i<Len; i++, j++)
{
Re = new RegExp(["\\{", j, "\\}"].join(""), "g");
sStr = sStr.replace(Re, arguments[i]);
}
return sStr;
}/* function format_f(sStr) */
var AbstractFactory_Interface =
new Interface
(
'AbstractFactory_Interface'
, {
CreateProductA:function(){}
, CreateProductB: function(){}
}
);var AbstractFactory =
{
id:"ok"
}AbstractFactory.prototype = {};
AbstractFactory.prototype.interface = AbstractFactory_Interface;function ConcreteFactory1()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
this.CreateProductA = function(){return new ProductA1();}
this.CreateProductB = function(){return new ProductB1();} if(this.interface) this.interface.valid(this);
}
ConcreteFactory1.prototype = AbstractFactory;function ConcreteFactory2()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
this.CreateProductA = function(){return new ProductA2();}
this.CreateProductB = function(){return new ProductB2();} if(this.interface) this.interface.valid(this);
}
ConcreteFactory2.prototype = AbstractFactory;
var AbstractProductB_Interface =
new Interface
(
'AbstractProductB_Interface'
, {
Interact:function(AbstractProductA_Instance){}
}
);var AbstractProductA =
{}
AbstractProductA.prototype = {};
var AbstractProductB =
{}
AbstractProductB.prototype = {};
AbstractProductB.prototype.interface = AbstractProductB_Interface;function ProductA1()
{
this.id = "ProductA1";
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
if(this.interface) this.interface.valid(this);
}
ProductA1.prototype = AbstractProductA;function ProductB1()
{
this.id = "ProductB1";
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
if(this.interface) this.interface.valid(this);
}
ProductB1.prototype = AbstractProductB;
ProductB1.prototype.Interact =
function(AbstractProductA_Instance)
{
document.write(this.id + " interacts with " + AbstractProductA_Instance.id, "<br/>");
};function ProductA2()
{
this.id = "ProductA2";
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
if(this.interface) this.interface.valid(this);
}
ProductA2.prototype = AbstractProductA;function ProductB2()
{
this.id = "ProductB2";
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
if(this.interface) this.interface.valid(this);
}
ProductB2.prototype = AbstractProductB;
ProductB2.prototype.Interact =
function(AbstractProductA_Instance)
{
document.write(this.id + " interacts with " + AbstractProductA_Instance.id, "<br/>");
};
function Environment(factory_instance)
{
var AbstractProductA_Instance = null;
var AbstractProductB_Instance = null;
AbstractProductA_Instance = factory_instance.CreateProductA();
//document.write("<h2>",AbstractProductA_Instance.id,"</h2>");
AbstractProductB_Instance = factory_instance.CreateProductB();
this.Run =
function()
{
AbstractProductB_Instance.Interact(AbstractProductA_Instance);
}
}var AbstractFactory_Instance = null;
var Environment_Instance = null;AbstractFactory_Instance = new ConcreteFactory1();
Environment_Instance = new Environment(AbstractFactory_Instance);
Environment_Instance.Run();AbstractFactory_Instance = new ConcreteFactory2();
Environment_Instance = new Environment(AbstractFactory_Instance);
Environment_Instance.Run();/*]]>*/
</script>
function Interface(name_, set_)
{/* shawl.qiu code */
var This = this;
if(!name_) { throw new Error('Interface property name_ is null!'); };
This.name = name_;
if(!set_) { throw new Error(format_f('from Interface "{0}", property set_ is null!', name_)); };
This.set = set_;
}/* function Interface(name_, set_) */Interface.prototype.valid = interface_valid_f;function interface_valid_f(class_ins)
{
var This = this;
for(var key_ in This.set)
{
var interface_item = This.set[key_];
var class_item = class_ins[key_];
if(typeof class_item=="undefined")
{
switch(typeof interface_item)
{
case "function":
throw new Error(format_f("Interface '{0}', method '{1}' is null!", This.name, key_));
break;
default:
if(typeof class_item!="number")
{
throw new Error(format_f("Interface '{0}', property '{1}' is null!", This.name, key_));
}
break;
}
}
else if(typeof interface_item!=typeof class_item)
{
throw new Error
(
format_f
(
"Interface '{0}', property '{1}' type {2} and instance class property '{1}' type {3}, type is not the same!"
, This.name
, key_
, typeof interface_item
, typeof class_item
)
);
}
}/* for(var key_ in This.set) */
return;
}/* function interface_valid_f(class_ins) */function format_f(sStr)
{/* shawl.qiu code, return string */
var Len = arguments.length, Re = null;
switch(Len)
{
case 0: return "";
case 1: return sStr;
}
for(var i=1, j=0; i<Len; i++, j++)
{
Re = new RegExp(["\\{", j, "\\}"].join(""), "g");
sStr = sStr.replace(Re, arguments[i]);
}
return sStr;
}/* function format_f(sStr) */
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>abstract class(js) - shawl.qiu template</title>
</head>
<body><script type="text/javascript">
/*<![CDATA[*/var Page =
{
Page_Id: "Page"
}function SkillsPage()
{
this.SkillsPage_Id = "SkillsPage";
}
SkillsPage.prototype = Page;var SkillsPage_instance = new SkillsPage();document.write(SkillsPage_instance.Page_Id, "<br/>");
document.write(SkillsPage_instance.SkillsPage_Id, "<br/>");/*]]>*/
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simiple Factory - shawl.qiu template</title>
</head>
<body><script type="text/javascript" src="./js/oop.js" ></script><script type="text/javascript">
/*<![CDATA[*/var my_interface =
new Interface
(
'my_interface'
, {
TurnOn:function(){}
, TurnOff: function(){}
}
);var TheLevel = this;function Light()
{
this.Light_Id = "Light";
}Light.prototype.interface = my_interface;Light.Creator =
function(light_name)
{
return new TheLevel[light_name];
}function BulbLight()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
if(this.interface) this.interface.valid(this);
this.BulbLight_Id = "BulbLight";
}BulbLight.prototype = new Light();
BulbLight.prototype.TurnOn = function(){document.write("Bulb Light is Turned on", "<br/>");}
BulbLight.prototype.TurnOff = function(){document.write("Bulb Light is Turned off", "<p/>");}
function TubeLight()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
if(this.interface) this.interface.valid(this);
this.TubeLight_Id = "TubeLight";
}TubeLight.prototype = new Light();
TubeLight.prototype.TurnOn = function(){document.write("Tube Light is Turned on", "<br/>");}
TubeLight.prototype.TurnOff = function(){document.write("Tube Light is Turned off", "<p/>");}var light_instance = null;light_instance = Light.Creator("BulbLight");
light_instance.TurnOn();
light_instance.TurnOff();light_instance = Light.Creator("TubeLight");
light_instance.TurnOn();
light_instance.TurnOff();
/*]]>*/
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Singleton - shawl.qiu template</title>
</head>
<body>
<script type="text/javascript">
/*<![CDATA[*/
var Singleton =
{
x: "x"
, y: "7"
, method:function(){document.write("Singleton.method");}
}/*
var instance_ = new Singleton(); // 这是错误的
因为 Singleton 是构造器,所以使用 new 关键字创建实例时会出错。
*/document.write(Singleton.x, "<br/>");
document.write(Singleton.y, "<br/>");
Singleton.method();/*]]>*/
</script>
</body>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Factory Method(example_document) - shawl.qiu template</title>
</head>
<body><script type="text/javascript" src="./js/oop.js" ></script><script type="text/javascript">
/*<![CDATA[*/var Page = {};
Page.prototype = {};function SkillsPage()
{
if(this.prototype.interface) this.prototype.interface.valid(this);
}
SkillsPage.prototype = Page;function EducationPage()
{
if(this.prototype.interface) this.prototype.interface.valid(this);
}
EducationPage.prototype = Page;function ExperiencePage()
{
if(this.prototype.interface) this.prototype.interface.valid(this);
}
ExperiencePage.prototype = Page;function IntroductionPage()
{
if(this.prototype.interface) this.prototype.interface.valid(this);
}
IntroductionPage.prototype = Page;function ResultsPage()
{
if(this.prototype.interface) this.prototype.interface.valid(this);
}
ResultsPage.prototype = Page;function ConclusionPage()
{
if(this.prototype.interface) this.prototype.interface.valid(this);
}
ConclusionPage.prototype = Page;function SummaryPage()
{
if(this.prototype.interface) this.prototype.interface.valid(this);
}
SummaryPage.prototype = Page;function BibliographyPage()
{
if(this.prototype.interface) this.prototype.interface.valid(this);
}
BibliographyPage.prototype = Page;
var Document_Interface =
new Interface
(
'Document_Interface'
, {
CreatePages:function(){}
}
);
var Document =
{
pages: []
}
Document.prototype = {};
Document.prototype.interface = Document_Interface;function Resume()
{
if(this.prototype.interface) this.prototype.interface.valid(this);
this.CreatePages();
}
Resume.prototype = Document;
Resume.prototype.CreatePages = Resume_createPages_f;function Resume_createPages_f()
{
this.pages.push(new SkillsPage());
this.pages.push(new EducationPage());
this.pages.push(new ExperiencePage());
}function Report()
{
if(this.prototype.interface) this.prototype.interface.valid(this);
this.CreatePages();
}
Report.prototype = Document;
Report.prototype.CreatePages = Report_createPages_f;function Report_createPages_f()
{
this.pages.push(new IntroductionPage());
this.pages.push(new ResultsPage());
this.pages.push(new ConclusionPage());
this.pages.push(new SummaryPage());
this.pages.push(new BibliographyPage());
}var document_list = [];
document_list.push(new Resume());
document_list.push(new Report());for(var document_key in document_list)
{
var document_item = document_list[document_key];
document.write("document: ", document_key, "<br/>");
for(var page_key in document_item)
{
var page_item = document_item[page_key];
document.write("page_key: ", page_key, "<br/>");
}
document.write("<p/>");
}/*]]>*/
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Factory Method - shawl.qiu template</title>
</head>
<body><script type="text/javascript" src="./js/oop.js" ></script><script type="text/javascript">
/*<![CDATA[*/var my_interface =
new Interface
(
'my_interface'
, {
TurnOn:function(){}
, TurnOff: function(){}
}
);var TheLevel = this;function Light()
{
this.Light_Id = "Light";
}Light.prototype.interface = my_interface;Light.Creator =
function(light_name)
{
return new TheLevel[light_name];
}function BulbLight()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
if(this.interface) this.interface.valid(this);
this.BulbLight_Id = "BulbLight";
}BulbLight.prototype = new Light();
BulbLight.prototype.TurnOn = function(){document.write("Bulb Light is Turned on", "<br/>");}
BulbLight.prototype.TurnOff = function(){document.write("Bulb Light is Turned off", "<p/>");}function TubeLight()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
if(this.interface) this.interface.valid(this);
this.TubeLight_Id = "TubeLight";
}TubeLight.prototype = new Light();
TubeLight.prototype.TurnOn = function(){document.write("Tube Light is Turned on", "<br/>");}
TubeLight.prototype.TurnOff = function(){document.write("Tube Light is Turned off", "<p/>");}
var creator_interface =
new Interface
(
'creator_interface'
, {
factory:function(){}
}
);function Creator()
{
this.Creator_Id = "Creator";
}Creator.prototype.interface = creator_interface;function BulbCreator()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
if(this.interface) this.interface.valid(this);
}BulbCreator.prototype = new Creator();
BulbCreator.prototype.factory = function(){ return new BulbLight(); };function TubeCreator()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
if(this.interface) this.interface.valid(this);
}TubeCreator.prototype = new Creator();
TubeCreator.prototype.factory = function(){ return new TubeLight(); };var creator_instance = null;
var light_instance = null;creator_instance = new BulbCreator();
light_instance = creator_instance.factory();
light_instance.TurnOn();
light_instance.TurnOff();creator_instance = new TubeCreator();
light_instance = creator_instance.factory();
light_instance.TurnOn();
light_instance.TurnOff();/*]]>*/
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>shawl.qiu template</title>
</head>
<body><script type="text/javascript">
/*<![CDATA[*/function obj()
{
Function.MakeChain(this);
}
obj.prototype.instance = "demo";
obj.prototype.fortest = function(){ document.write(this.instance, "<br/>");};function str()
{
this.temp = "temp";
}
obj.prototype.str = str;
str.prototype.text = function(){ document.write(this.Parent.instance, "<br/>"); }Function.prototype.MakeChain = fMakeChainX;
function fMakeChainX(Obj)
{/* shawl.qiu code, void return */
/* using: fMakeChainX(this); | Function.MakeChain(this); */
for(var i in Obj)
{
if(typeof(Obj[i])=="function" && (/\bthis\.\b/.test(Obj[i]) || /Function\.MakeChain/.test(Obj[i])))
{
var Count = 0;
for(var j in Obj[i].prototype){Count++; break;}
if(/this\.\S+\s*\=/.test(Obj[i].toString())||Count>0)
{
Obj[i].prototype.Parent = Obj;
Obj[i] = new Obj[i];
arguments.callee(Obj[i]);
return;
}
}
}/* for(var i in Obj) */
}/* function fMakeChainX(Obj) */var obj_instance = new obj();
obj_instance.str.text();/*]]>*/
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javascript interface example - shawl.qiu template</title><script type="text/javascript" src="./js/oop.js" ></script><script type="text/javascript">
/*<![CDATA[*/var my_interface =
new Interface
(
'my_interface'
, {
m_1:function(){}
, m_2: function(){}
, m_3: 0
, m_4: function(){}
, p_1: 0
, p_2: "0"
}
);Test_Class.prototype =
{
m_1: function(){}
, m_2: function(){}
, m_3: 0
, m_4: function(){}
, p_1: 0
, p_2: "0"
, interface: my_interface
}function Test_Class()
{
if(this.interface) this.interface.valid(this);
var This = this;
}var test_class = new Test_Class();
/*]]>*/
</script>
</body>
</html>
和
Prototype_Method_2.prototype实际上都指写了 obj感谢 王辰。有时间的话我会把 js 实现的设计模式放到这里
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>abstract factory(continent example) - shawl.qiu template</title>
</head>
<body><script type="text/javascript" src="./js/oop.js" ></script><script type="text/javascript">
/*<![CDATA[*/var ContinentFactory_Interface =
new Interface
(
'ContinentFactory_Interface'
, {
CreateHerbivore:function(){}
, CreateCarnivore: function(){}
}
);
var ContinentFactory =
{}
ContinentFactory.prototype = {};
ContinentFactory.prototype.interface = ContinentFactory_Interface;function AfricaFactory()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
this.CreateHerbivore =
function()
{
return new Wildebeest();
};
this.CreateCarnivore =
function()
{
return new Lion();
}; if(this.interface) this.interface.valid(this);
}
AfricaFactory.prototype = ContinentFactory;function AmericaFactory()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
this.CreateHerbivore =
function()
{
return new Bison();
};
this.CreateCarnivore =
function()
{
return new Wolf();
}; if(this.interface) this.interface.valid(this);
}
AmericaFactory.prototype = ContinentFactory;var Herbivore = {};
Herbivore.prototype = {};var Carnivore_Interface =
new Interface
(
'Carnivore_Interface'
, {
Eat:function(Herbivore_Instance){}
}
);var Carnivore = {};
Carnivore.prototype = {};
Carnivore.prototype.interface = Carnivore_Interface;function Wildebeest()
{
this.id = "Wildebeest";
this.constructor = arguments.callee
this.parent = this.constructor.prototype; if(this.interface) this.interface.valid(this);
}
Wildebeest.prototype = Herbivore;
function Lion()
{
this.id = "Lion";
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
this.Eat =
function (Herbivore_Instance)
{
document.write(this.id, " eats ", Herbivore_Instance.id, "<br/>");
}; if(this.interface) this.interface.valid(this);
}
Lion.prototype = Carnivore;
Lion.prototype.interface = Carnivore_Interface;function Bison()
{
this.id = "Bison";
this.constructor = arguments.callee
this.parent = this.constructor.prototype; if(this.interface) this.interface.valid(this);
}
Bison.prototype = Herbivore;function Wolf()
{
this.id = "Wolf";
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
this.Eat =
function (Herbivore_Instance)
{
document.write(this.id, " eats ", Herbivore_Instance.id, "<br/>");
}; if(this.interface) this.interface.valid(this);
}
Wolf.prototype = Carnivore;
Wolf.prototype.interface = Carnivore_Interface;function AnimalWorld(ContinentFactory_Instance)
{
var Herbivore_Instance = null;
var Carnivore_Instance = null;
Herbivore_Instance = ContinentFactory_Instance.CreateHerbivore();
Carnivore_Instance = ContinentFactory_Instance.CreateCarnivore();
this.RunFoodChain =
function()
{
Carnivore_Instance.Eat(Herbivore_Instance);
}
}var ContinentFactory_Instance = null;
var AnimalWorld_Instance = null;ContinentFactory_Instance = new AfricaFactory();
AnimalWorld_Instance = new AnimalWorld(ContinentFactory_Instance);
AnimalWorld_Instance.RunFoodChain();ContinentFactory_Instance = new AmericaFactory();
AnimalWorld_Instance = new AnimalWorld(ContinentFactory_Instance);
AnimalWorld_Instance.RunFoodChain();/*]]>*/
</script>
</body>
</html>
改写 function 的默认 toString() 函数<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>abstract factory(continent example) - shawl.qiu template</title>
</head>
<body><script type="text/javascript" src="./js/oop.js" ></script><script type="text/javascript">
/*<![CDATA[*/var ContinentFactory_Interface =
new Interface
(
'ContinentFactory_Interface'
, {
CreateHerbivore:function(){}
, CreateCarnivore: function(){}
}
);
var ContinentFactory =
{}
ContinentFactory.prototype = {};
ContinentFactory.prototype.interface = ContinentFactory_Interface;function AfricaFactory()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
this.CreateHerbivore =
function()
{
return new Wildebeest();
};
this.CreateCarnivore =
function()
{
return new Lion();
}; if(this.interface) this.interface.valid(this);
}
AfricaFactory.prototype = ContinentFactory;function AmericaFactory()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
this.CreateHerbivore =
function()
{
return new Bison();
};
this.CreateCarnivore =
function()
{
return new Wolf();
}; if(this.interface) this.interface.valid(this);
}
AmericaFactory.prototype = ContinentFactory;var Herbivore = {};
Herbivore.prototype = {};var Carnivore_Interface =
new Interface
(
'Carnivore_Interface'
, {
Eat:function(Herbivore_Instance){}
}
);var Carnivore = {};
Carnivore.prototype = {};
Carnivore.prototype.interface = Carnivore_Interface;function Wildebeest()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
this.toString = function(){return "Wildebeest";}; if(this.interface) this.interface.valid(this);
}
Wildebeest.prototype = Herbivore;
function Lion()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
this.toString = function(){return "Lion";};
this.Eat =
function (Herbivore_Instance)
{
document.write(this, " eats ", Herbivore_Instance, "<br/>");
}; if(this.interface) this.interface.valid(this);
}
Lion.prototype = Carnivore;
Lion.prototype.interface = Carnivore_Interface;function Bison()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
this.toString = function(){return "Bison";}; if(this.interface) this.interface.valid(this);
}
Bison.prototype = Herbivore;function Wolf()
{
this.constructor = arguments.callee
this.parent = this.constructor.prototype;
this.toString = function(){return "Wolf";};
this.Eat =
function (Herbivore_Instance)
{
document.write(this, " eats ", Herbivore_Instance, "<br/>");
}; if(this.interface) this.interface.valid(this);
}
Wolf.prototype = Carnivore;
Wolf.prototype.interface = Carnivore_Interface;function AnimalWorld(ContinentFactory_Instance)
{
var Herbivore_Instance = null;
var Carnivore_Instance = null;
Herbivore_Instance = ContinentFactory_Instance.CreateHerbivore();
Carnivore_Instance = ContinentFactory_Instance.CreateCarnivore();
this.RunFoodChain =
function()
{
Carnivore_Instance.Eat(Herbivore_Instance);
}
}var ContinentFactory_Instance = null;
var AnimalWorld_Instance = null;ContinentFactory_Instance = new AfricaFactory();
AnimalWorld_Instance = new AnimalWorld(ContinentFactory_Instance);
AnimalWorld_Instance.RunFoodChain();ContinentFactory_Instance = new AmericaFactory();
AnimalWorld_Instance = new AnimalWorld(ContinentFactory_Instance);
AnimalWorld_Instance.RunFoodChain();/*]]>*/
</script>
</body>
</html>
这样才是继承的写法吧
但我需要 Prototype_Method_1 和 Prototype_Method_2 各有自己的特有方法。所以对JS来说也只能用 this.method = function
我要的是抽象类, 不是类。
也就是要 不能 new obj()
但又要可以获取他的结构
var obj = function(){};Prototype_Method_1.prototype = new obj();Prototype_Method_2.prototype = new obj();
这样才对吧
不然怎么叫继承
那样的话
prototype_Method_1 和 prototype_Method_2 的 prototype 就都指向 obj 了prototype_Method_1.method
和
prototype_Method_2.method
其实都是 obj.method,
也就不是独立了而且 new obj 本来就是对象,没有 constrctor(构造器)
那就是我一楼遇到的问题你自己慢慢想吧,不回了。
var i,p,op=this.prototype,a=function(){};
a.prototype=SuperClass.prototype;
p=this.prototype=new a();
for(i in op)p[i]=op[i];
this.prototype.constructor=op.constructor;op=null;return p;
};ChildClass.Extends(ParentClass); 这样再加上构造器继承即可以达到完美的继承。
修正版见:
http://www.btbtd.org/test/sqJsTools/topic/code_javascript/list.shtml查找 设计模式
/*<![CDATA[*/var obj ={};function a(){
this.id = "a";
}
a.prototype = obj;//这样只是使得a.prototype成为了obj的引用
a.prototype.method = function(){//将设置obj.method
document.write(this.id+"<br/>");
}alert(obj.method);function b(){
this.id = "b";
}
b.prototype = obj;//这样只是使得b.prototype成为了obj的引用
b.prototype.method = function(){//将设置obj.method
//document.write(this.id+"<br/>");
}alert(obj.method);//obj.method保留了最后一次被修改的状态var instance = null;
instance = new a();
instance.method();
instance = new b();
instance.method();/*]]>*/
</script>解决方法,只要设置a,b的prototype为一个Object的实例并继承(抄写)原obj的内容就好了,如:<script type="text/javascript">
/*<![CDATA[*/var obj ={x:'x'};function a(){
this.id = "a";
}
a.prototype = function(o){var oo=new Object;for(var i in o){oo[i]=o[i]};return oo}(obj);
a.prototype.method = function(){
document.write(this.id+','+this.x+'<br/>');
}function b(){
this.name = "b";
}
b.prototype = function(o){var oo=new Object;for(var i in o){oo[i]=o[i]};return oo}(obj);
b.prototype.method = function(){
document.write(this.name+','+this.x+'<br/>');
}var instance = null;
instance = new a();
instance.method();
instance = new b();
instance.method();/*]]>*/
</script>