javasript 面向对象
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;
}
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);
轉載于:https://www.cnblogs.com/skyangell/archive/2008/03/08/1096416.html
總結
以上是生活随笔為你收集整理的javasript 面向对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 教你怎么学JAVA+Java入门项目(可
- 下一篇: 慕课软件质量保证与测试(第八章.软件评审