面向对象的JavaScript
JavaScript 是一种解释型的、基于对象的脚本语言。尽管与 C++、C# 这样成熟的面向对象的语言相比,JavaScript的功能要弱一些,但对于它的预期用途而言,JavaScript的功能已经足够大了。但是由于各种各样的原因,我们在实际进行开发的过程中往往忽略了他基于对象的这一特性,以至JavaScript编写的程序显的杂乱无章。这样不仅不易于观看,更不易于修改。今天就让我们来看看JavaScript基于对象的这一特性。
类:
委托:
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();
枚举:
function _StatusList(){
var object = new Object();
object.正常= "Normal";
object.删除= "Delete";
object.审核通过= "Auditing";
object.驳回 = "OverRule";
return object;
}
Object.prototype.StatusList = new _StatusList();
function TObject(){
var obj = new Object();
obj.Type = "YiZhu";
obj.Status = Object.StatusList.审核通过;
}
alert(obj.Status);
类:
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();
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();
枚举:
function _StatusList(){
var object = new Object();
object.正常= "Normal";
object.删除= "Delete";
object.审核通过= "Auditing";
object.驳回 = "OverRule";
return object;
}
Object.prototype.StatusList = new _StatusList();
function TObject(){
var obj = new Object();
obj.Type = "YiZhu";
obj.Status = Object.StatusList.审核通过;
}
alert(obj.Status);