类:
function DelegateObject(){
var obj = new Object();
obj.value = "";
obj.FormatString = null;
obj.toString = function _toString(){
if(obj.FormatString != null)
return this.FormatString(this.Value);
else
return this.Value;
}
return obj;
}
var obj = new DelegateObject();
委托:
function DelegateObject(){
var obj = new Object();
obj.value = "";
obj.FormatString = null;
obj.toString = function _toString(){
if(obj.FormatString != null)
return this.FormatString(this.Value);
else
return this.Value;
}
return obj;
}
function ConvertToString(value){
return "Result:" + value;
}
var obj = new DelegateObject();
obj.Value = "Hello World!";
obj.FormatString = ConvertToString;
document.write(obj.toString());
重写:
function DelegateObject(){
var obj = new Object();
obj.toString = function _toString(){
if(obj.FormatString != null)
return this.FormatString(this.Value);
else
return this.Value;
}
return obj;
}
继承:
function DelegateObject(){
var obj = new Object();
obj.value = "";
obj.FormatString = null;
obj.toString = function _toString(){
if(obj.FormatString != null)
return this.FormatString(this.Value);
else
return this.Value;
}
return obj;
}
function Class2(){
var obj = new DelegateObject();
return obj;
}
function ConvertTOString(value){
return "Result:" + value;
}
var obj = new Class2();
obj.Value = "Hello World!";
obj.FormatString = ConvertTOString;
document.write(obj.toString());
事件:
function EventHandler(){
var eventobj = new Object();
eventobj._eventHandler = null;
eventobj.Activate = function _activate(){
if(eventobj._eventHandler != null)
eventobj._eventHandler();
}
eventobj.Add = function _add(eventHandler){
eventobj._eventHandler = EventHandler;
}
eventobj.Remove = function _remove(){
eventobj._eventHandler = null;
}
return eventobj;
}
function mouseClick(){
alert("Hello World!");
}
var obj = new EventHandler();
obj.Add(mouseClick());
obj.Activate();