Microsoft AJAX Library对 Object的扩展

向基 ECMAScript (JavaScript) Object 对象提供扩展的类似反射的功能。

Object 扩展是 Microsoft AJAX Library 的一部分。这些扩展为内置的 JavaScript Object 对象添加了功能。 Object 扩展提供有关类型化实例的类似反射的信息。使用这些方法可发现对象的类型和类型名称。

Object.getType 函数

返回指定对象实例的类型。

使用 getType 函数可以获取表示对象的运行时类型的新类型实例。

 

/* param
instance:要返回其类型的对象

return:一个类型示例,表示 instance 的运行时对象
*/
var typeVar = Object.getType(instance);

 

Object.getTypeName 函数

返回标识对象的运行时类型名称的字符串。

使用 getTypeName 函数可确定对象的运行时类型名称。类型名称是以字符串的形式返回的,该字符串表示完全限定类型名称。

 

/* param
instance:要为其返回运行时类型名称的对象。

return:一个字符串,标识 instance 的运行时类型名称
*/
var typeNameVar = Object.getTypeName(instance);

 

示例代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Samples</title>
</head>
<body>
    <form id="form1" runat="server">
       <asp:ScriptManager runat="server" ID="ScriptManager1">
       </asp:ScriptManager>
       <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);
                // Output "The type name of object a is: Samples.Rectangle"
                alert("The type name of object a is: " + name);
                
                var isSquare = Samples.Rectangle.isInstanceOfType(b)
                // Output "Object b is an instance of type Square: false"
                alert("Object b is an instance of type Square: " + isSquare);
                
                var c = Object.getType(b);
                
                name = Object.getTypeName(c);
                 // Output "The type name of object c is: Function"
                alert("The type name of object c is: " + name);
                        
                var isSquare = Samples.Square.isInstanceOfType(c);
                if (isSquare)
                {
                   var newLength = a.getWidth();
                   c.setLength(newLength);
                   alert("Object c is a Square with a length of: " + c.getLength());
                }
            }

            // Run the sample.
            Samples.testObjectReflection();

        </script>

    </form>
</body>
</html>

 

posted @ 2013-01-06 16:15  拓荒者FF  阅读(236)  评论(0编辑  收藏  举报