Object 类型扩展
提供了基于 ECMAScript (JavaScript) 的Object 对象的扩展.
命名空间: 无. 本扩展类型是全局型的不属于任意一命名空间。
继承: 无.
语法
var objectVar = new Object();
成员扩展
名称 | 说明 |
---|---|
Object.getType 函数 | 返回一个指定类型实例. |
Object.getTypeName 函数 | 返回当前运行对象的类型名。 |
备注
Object 扩展是Microsoft AJAX库的一部分。 它为JavaScript 类库添加了许多功能,并且使之与.net更加亲和。 Object类型扩展提供了更多关于类型的描述与操作方法.
更多关于 JavaScript 类的扩展类型的消息, 请参见 Object Object in the JScript Language Reference.
下面的示例展示了如何使用Object类型扩展,使用getType 扩展函数来创建并引用一个实例。它也用到了getTypeName 扩展函数。
CS
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1">
</asp:ScriptManager>
</form>
</body>
</html>
<script type="text/javascript">
Type.registerNamespace('Samples');
// Define and register a Samples.Rectangle class.
Samples.Rectangle = function(width, height)
{
this._width = width;
this._height = height;
}
Samples.Rectangle.prototype.getWidth = function() {
return (this._width === undefined) ? null : this._width;
}
Samples.Rectangle.prototype.getHeight = function() {
return (this._width === undefined) ? null : this._height;
}
Samples.Rectangle.registerClass('Samples.Rectangle');
// Define and register a Samples.Square class.
Samples.Square = function(length)
{
this._length = length;
}
Samples.Square.prototype.getLength = function() {
return (this._length === undefined) ? null : this._length;
}
Samples.Square.prototype.setLength = function(length) {
this._length = length;
}
Samples.Square.registerClass('Samples.Square');
// Create instances of Square and Rectangle and discover their types.
Samples.testObjectReflection = function()
{
var width = 200;
var height = 100;
var a = new Samples.Rectangle(width, height);
var length = 50;
var b = new Samples.Square(length);
var name = Object.getTypeName(a);
document.write("The type name of object /"a/" is: " + name + "</p>");
var isSquare = Samples.Rectangle.isInstanceOfType(b)
document.write("Object /"b/" is an instance of type Square: " + isSquare + "</p>");
var c = Object.getType(b);
name = Object.getTypeName(c);
document.write("The type name of object /"c/" is: " + name + "</p>");
var isSquare = Samples.Square.isInstanceOfType(c);
if (isSquare)
{
var newLength = a.getWidth();
c.setLength(newLength);
document.write("Object /"c/" is a Square with a length of: " + c.getLength() + "</p>");
}
}
// Run the sample.
Samples.testObjectReflection();
</script>
VB
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1">
</asp:ScriptManager>
</form>
</body>
</html>
<script type="text/javascript">
Type.registerNamespace('Samples');
// Define and register a Samples.Rectangle class.
Samples.Rectangle = function(width, height)
{
this._width = width;
this._height = height;
}
Samples.Rectangle.prototype.getWidth = function() {
return (this._width === undefined) ? null : this._width;
}
Samples.Rectangle.prototype.getHeight = function() {
return (this._width === undefined) ? null : this._height;
}
Samples.Rectangle.registerClass('Samples.Rectangle');
// Define and register a Samples.Square class.
Samples.Square = function(length)
{
this._length = length;
}
Samples.Square.prototype.getLength = function() {
return (this._length === undefined) ? null : this._length;
}
Samples.Square.prototype.setLength = function(length) {
this._length = length;
}
Samples.Square.registerClass('Samples.Square');
// Create instances of Square and Rectangle and discover their types.
Samples.testObjectReflection = function()
{
var width = 200;
var height = 100;
var a = new Samples.Rectangle(width, height);
var length = 50;
var b = new Samples.Square(length);
var name = Object.getTypeName(a);
document.write("The type name of object /"a/" is: " + name + "</p>");
var isSquare = Samples.Rectangle.isInstanceOfType(b)
document.write("Object /"b/" is an instance of type Square: " + isSquare + "</p>");
var c = Object.getType(b);
name = Object.getTypeName(c);
document.write("The type name of object /"c/" is: " + name + "</p>");
var isSquare = Samples.Square.isInstanceOfType(c);
if (isSquare)
{
var newLength = a.getWidth();
c.setLength(newLength);
document.write("Object /"c/" is a Square with a length of: " + c.getLength() + "</p>");
}
}
// Run the sample.
Samples.testObjectReflection();
</script>