簡述迷你MVVM架構avalon在相容舊式IE所做的努力

來源:互聯網
上載者:User

很多時候,寫代碼就像砌磚頭,只要我們不關心蓋樓的原因、建築的原理、土木工程基礎和工程經驗,就算我們砌了100棟高樓,我們也就只是一個砌磚工人,永遠也成為不了一個工程師,更別說建築師了。而那些包工頭也只會把我們當成勞動力罷了。——左耳朵耗子

avalon在相容舊式IE上做了大量工作,從而讓它更接地氣,完美地運行於國內的各種奇葩瀏覽器中。

首先是Object.defineProperties的類比,正因為有這東西,才能讓avalon是純事件驅動地同步視圖,而不用髒檢測,從而獲得更高的效能。

//IE6-8使用VBScript類的set get語句實現
if (!defineProperties && window.VBArray) {
window.execScript([
"Function parseVB(code)",
"\tExecuteGlobal(code)",
"End Function"
].join("\n"), "VBScript")

function VBMediator(accessingProperties, name, value) {
var accessor = accessingProperties[name]
if (arguments.length === 3) {
accessor(value)
} else {
return accessor()
}
}
defineProperties = function(name, accessingProperties, normalProperties) {
var className = "VBClass" + setTimeout("1"),
buffer = []
buffer.push(
"Class " + className,
"\tPrivate [__data__], [__proxy__]",
"\tPublic Default Function [__const__](d, p)",
"\t\tSet [__data__] = d: set [__proxy__] = p",
"\t\tSet [__const__] = Me", //鏈式調用
"\tEnd Function")
//添加普通屬性,因為VBScript對象不能像JS那樣隨意增刪屬性,必須在這裡預先定義好
for (name in normalProperties) {
buffer.push("\tPublic [" + name + "]")
}
buffer.push("\tPublic [" + 'hasOwnProperty' + "]")
//添加訪問器屬性
for (name in accessingProperties) {
if (!(name in normalProperties)) { //防止重複定義
buffer.push(
//由於不知對方會傳入什麼,因此set, let都用上
"\tPublic Property Let [" + name + "](val" + expose + ")", //setter
"\t\tCall [__proxy__]([__data__], \"" + name + "\", val" + expose + ")",
"\tEnd Property",
"\tPublic Property Set [" + name + "](val" + expose + ")", //setter
"\t\tCall [__proxy__]([__data__], \"" + name + "\", val" + expose + ")",
"\tEnd Property",
"\tPublic Property Get [" + name + "]", //getter
"\tOn Error Resume Next", //必須優先使用set語句,否則它會誤將數組當字串返回
"\t\tSet[" + name + "] = [__proxy__]([__data__],\"" + name + "\")",
"\tIf Err.Number <> 0 Then",
"\t\t[" + name + "] = [__proxy__]([__data__],\"" + name + "\")",
"\tEnd If",
"\tOn Error Goto 0",
"\tEnd Property")
}
}
buffer.push("End Class") //類定義完畢
buffer.push(
"Function " + className + "Factory(a, b)", //建立執行個體並傳入兩個關鍵的參數
"\tDim o",
"\tSet o = (New " + className + ")(a, b)",
"\tSet " + className + "Factory = o",
"End Function")
window.parseVB(buffer.join("\r\n")) //先建立一個VB類工廠
return window[className + "Factory"](accessingProperties, VBMediator) //得到其產品
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.