枚举是指包含一组命名整数常量的类。 您可以像访问属性那样访问这些值。
myObject.color = myColorEnum.red
枚举提供不同于整数的另一种易读的表示形式。
MyNamespace.MyFlags = function() {
throw Error.notImplemented();
}
MyNamespace.MyFlags.prototype = {
Item1 : 1,
Item2 : 2,
Item3 : 4,
None : 0,
All : 7
}
MyNamespace.MyFlags.registerEnum("MyNamespace.MyFlags", true);
注册枚举使用registerEnum方法,第二个参数是判断是否是标记类型类型,可缺省,如果是一个标记,那么内部成员的值按照2的次方赋予。枚举类型是没有构造函数的,所以构造函数抛出未实现异常。枚举类型的toString方法有两种,一种是prototype的toString方法,没有参数,它是直接将此枚举对象转化为数字,然后作为字符串输出。另一种是枚举类型中的静态方法,需要传递一个参数,参数类型为枚举类型,它转换这个枚举对象为枚举的描述。枚举类型的parse方法如下
MyNamespace.MyFlags.parse("Item1,Item2");
如果参数的字符串中只有Item1,那么表示把Item1字符串转换为一个标记或一个枚举类型,但如果有多个参数,那么是将两个值‘或’或者‘加起来’
下面的代码定义一个命名颜色的枚举,这些命名颜色用于表示十六进制的值。
Type.registerNamespace("Demo");
Demo.Color = function(){};
Demo.Color.prototype =
{
Red: 0xFF0000,
Blue: 0x0000FF,
Green: 0x00FF00,
White: 0xFFFFFF
}
Demo.Color.registerEnum("Demo.Color");
枚举
枚举的定义
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<script src="http://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" type="text/javascript"></script>
<script type="text/javascript">
Type.registerNamespace("jweaving.tutorials");
jweaving.tutorials.Style = function() {
};
jweaving.tutorials.Style.prototype = {
NORMAL : 1,
BOLD : 2,
ITALIC : 3,
UNDERLINE : 4
}
jweaving.tutorials.Style.registerEnum("jweaving.tutorials.Style");
jweaving.tutorials.TextBlock = function(name, text) {
this._name = name;
this._text = text;
this._color = "#000000";
this._style = "normal";
};
jweaving.tutorials.TextBlock.prototype = {
getName : function() {
return this._name
},
getText : function() {
return this._text
},
getColor : function() {
return this._color
},
setColor : function(color) {
this._color = color
},
getStyle : function() {
return this._style
},
setStyle : function(style) {
this._style = style
},
write : function() {
if(this._style == jweaving.tutorials.Style.BOLD) {
document.write(String.format('<div><b name="{0}" style="color:{1};" >{2}</b></div>', this._name, this._color, this._text));
} else if(this._style == jweaving.tutorials.Style.ITALIC) {
document.write(String.format('<div><i name="{0}" style="color:{1};" >{2}</i></div>', this._name, this._color, this._text));
} else if(this._style == jweaving.tutorials.Style.UNDERLINE) {
document.write(String.format('<div><u name="{0}" style="color:{1};" >{2}</u></div>', this._name, this._color, this._text));
} else {
document.write(String.format('<div name="{0}" style="color:{1};" >{2}</div>', this._name, this._color, this._text));
}
}
}
jweaving.tutorials.TextBlock.registerClass('jweaving.tutorials.TextBlock');
jweaving.tutorials.Button = function(name, text) {
this._name = name;
this._text = text;
};
jweaving.tutorials.Button.prototype = {
getName : function() {
return this._name
},
getText : function() {
return this._text
},
toString : function() {
return 'name:' + this._name + ',' + 'text:' + this._text;
}
}
jweaving.tutorials.Button.registerClass('jweaving.tutorials.Button');
jweaving.tutorials.IAction = function() {
}
jweaving.tutorials.IAction.Prototype = {
show : function() {
},
execute : function(textBlock) {
}
}
jweaving.tutorials.IAction.registerInterface('jweaving.tutorials.IAction');
jweaving.tutorials.StyleButton = function(name, text, style) {
jweaving.tutorials.StyleButton.initializeBase(this, [name, text]);
this._style = style;
}
jweaving.tutorials.StyleButton.prototype = {
getStyle : function() {
return this._style
},
setStyle : function(style) {
this._style = style;
},
toString : function() {
return jweaving.tutorials.StyleButton.callBaseMethod(this, 'toString') + ',' + 'style:' + this._style;
},
show : function() {
alert('样式' + this._style);
},
execute : function(textBlock) {
textBlock.setStyle(this._style);
}
}
jweaving.tutorials.StyleButton.registerClass('jweaving.tutorials.StyleButton', jweaving.tutorials.Button, jweaving.tutorials.IAction);
jweaving.tutorials.ColorButton = function(name, text, style, color) {
jweaving.tutorials.ColorButton.initializeBase(this, [name, text, style]);
this._color = color;
}
jweaving.tutorials.ColorButton.prototype = {
getColor : function() {
return this._color
},
setColor : function(color) {
this._color = color;
},
toString : function() {
return jweaving.tutorials.ColorButton.callBaseMethod(this, 'toString') + ',' + 'color:' + this._color;
},
show : function() {
alert('颜色' + this._color);
},
execute : function(textBlock) {
jweaving.tutorials.ColorButton.callBaseMethod(this, 'execute', [textBlock]);
textBlock.setColor(this._color);
}
}
jweaving.tutorials.ColorButton.registerClass('jweaving.tutorials.ColorButton', jweaving.tutorials.StyleButton);
</script>
</head>
<body>
<script type="text/javascript">
var button1 = new jweaving.tutorials.Button('button1', '普通按钮');
alert(button1.getText());
alert(button1);
alert(Object.getType(button1).getName());
processButton(button1);
var button2 = new jweaving.tutorials.StyleButton('button2', '样式按钮', jweaving.tutorials.Style.BOLD);
alert(button2.getText());
alert(button2);
alert(Object.getType(button2).getName());
if(jweaving.tutorials.Button.isInstanceOfType(button2)) {
alert(button2.getName() + ' is a Button instance.\r\nText property: ' + button2.getText());
}
if(jweaving.tutorials.IAction.isImplementedBy(button2)) {
alert(button2.getName() + ' implements IAction.\r\nText property: ' + button2.getText());
}
processButton(button2);
var button3 = new jweaving.tutorials.ColorButton('button3', '颜色按钮', jweaving.tutorials.Style.BOLD, 'Red');
alert(button3.getText());
alert(button3);
alert(Object.getType(button3).getName());
if(jweaving.tutorials.Button.isInstanceOfType(button3)) {
alert(button3.getName() + ' is a Button instance.\r\nText property: ' + button3.getText());
}
if(jweaving.tutorials.IAction.isImplementedBy(button3)) {
alert(button3.getName() + ' implements IAction.\r\nText property: ' + button3.getText());
}
processButton(button3);
var textBlock1 = new jweaving.tutorials.TextBlock('textBlock1', 'Hello World!');
textBlock1.write();
button2.execute(textBlock1);
textBlock1.write();
button3.execute(textBlock1);
textBlock1.write();
function processButton(button) {
alert('Current Button ' + button.getName());
if(jweaving.tutorials.IAction.isImplementedBy(button)) {
button.show();
}
}
</script>
</body>
</html>
Microsoft AJAX Library Cheat Sheet——ASP.NET AJAX客户端框架的快速参考系列:http://www.cnblogs.com/dflying/archive/2007/02/09/639638.html
Microsoft AJAX Library Cheat Sheet ——ASP.NET AJAX客户端框架的快速参考:http://www.cnblogs.com/allnen/archive/2009/03/23/1419490.html
在Microsoft AJAX Library下JavaScript的面向对象开发:http://www.cnblogs.com/beniao/archive/2008/06/08/1204388.html
Microsoft Ajax:http://msdn.microsoft.com/zh-cn/library/ee341002.aspx
使用 Microsoft Ajax Library 创建自定义客户端脚本:http://msdn.microsoft.com/zh-cn/library/bb386453.aspx
领先技术: 深入了解 Microsoft AJAX Library:http://msdn.microsoft.com/zh-cn/magazine/cc163300.aspx
Microsoft AJAX Library Cheat Sheets:http://aspnetresources.com/blog/ms_ajax_cheat_sheets_batch2
ASP.NET AJAX深入浅出系列课程:http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/series/ASP_Ajax.aspx http://msdnwebcast.net/webcast/4/1957/