Ms Ajax Lib-> Object 类型扩展

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>

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值