如下,我們來定義Week的列舉:
var WeekDay = {};
WeekDay.Sunday = 0;
WeekDay.Monday = 1;
WeekDay.Tuesday = 2;
WeekDay.Wedesay = 3;
WeekDay.Thursday = 4;
WeekDay.Friday = 5;
WeekDay.Saturday = 6;
}
當然,我們有更直覺的方式。以定義DOM文檔節點類型為例,定義方式如下:
if(typeof Node == "undefined"){
var Node = {
ELEMENT_NODE: 1,
ATTRIBUTE_NODE: 2,
TEXT_NODE: 3,
CDATA_SECTION_NODE: 4,
ENTITY_REFERENCE_NODE: 5,
ENTITY_NODE: 6,
PROCESSING_INSTRUCTION_NODE: 7,
COMMENT_NODE: 8,
DOCUMENT_NODE: 9,
DOCUMENT_TYPE_NODE: 10,
代碼如下:
複製程式碼
程式碼如下:if(type Color = = "undefined"){
var Color = {
Color1: 'red',
Color4: 'black'
}
複製程式碼
程式碼如下:if(typeof PersonList == "undefined"){
var PersonList = {
ZhangSan: {
Id: 1,
Name: 'ZhangSan',
Gender: 'man'
},
LiSi: {
Id: 2,
Name: 'LiSi',
Gender: 'woman'
},
ZhaoWu: {
Id: 3,
}
}}測試結果如下圖: