物件導向的JavaScript

來源:互聯網
上載者:User
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);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.