Оснастки расширений объектных типов

Обновлен: Ноябрь 2007

Предоставляет расширенные функции типа отражения для базового объекта ECMAScript (JavaScript) Object.

Пространство имен: отсутствует. Данное расширение типа является глобальным и не принадлежит к пространству имен.

Наследование от: отсутствует

var objectVar = new Object();

Расширения членов.

Имя

Описание

Функция Object.getType

Возвращает тип заданного экземпляра объекта.

Функция Object.getTypeName

Возвращает строку, которая идентифицирует имя типа времени выполнения для объекта.

Заметки

Расширения Object являются частью библиотеки Microsoft AJAX (библиотека). Они добавляют функции встроенного объекта Java Script Object. Расширения Object предоставляют сведения типа отражения о типизированном экземпляре. Данные методы можно использовать для определения типа и имени типа объекта.

Дополнительные сведения об объекте JavaScript, который расширяет этот тип, и о его конструкторе, см. в подразделе Объект Object раздела Справочник по языку.

Пример

В следующем примере показан способ использования расширения Object. В данном коде создается новый экземпляр Objectи вызывается расширение функции getType для доступа к экземпляру типа, представляющему объект. В нем также вызывается расширение функции getTypeName для доступа к имени типа.

<!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>


См. также

Ссылки

Объект Object

Оператор new

Функции объектного типа

Другие ресурсы

Справочник по языку