Asp.net AJAX JavaScript 基本类型扩展(6)——Object 类型及其扩展

参考:
JScript  语言参考
http://msdn2.microsoft.com/en-us/library/16824cb5-6f1d-4b40-87fc-c7fb88da734c
Asp.net AJAX 在线文档
http://www.asp.net/AJAX/Documentation/Live/ClientReference/Global/JavascriptTypeExtensions/ObjectTypeExt/default.aspx

Asp.net AJAX JavaScript Object 类型扩展

getType 方法
 返回指定对象的实例
 
getTypeName Function
 返回指定对象的实例名

例:

<!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 基本类型

Object 对象
提供所有 JScript 对象通用的功能。

obj = new Object([value])

参数
obj

必选项。要赋值为 Object 对象的变量名。

value

可选项。任意一种 JScript 基本数据类型。(Number、Boolean、或 String。)如果 value 为一个对象,返回不作改动的该对象。如果 value 为 null、undefined,或者没有给出,则产生没有内容的对象。

说明
Object 对象被包含在所有其它 JScript 对象中;在所有其它对象中它的方法和属性都是可用的。在用户定义的对象中可以重定义这些方法,并在适当的时候通过 JScript 调用。toString 方法是经常被重定义的 Object 方法的例子。

在本语言参考中,每个 Object 方法的说明包括默认的和与对象相关固有 JScript 对象的实现信息。

属性
prototype 属性 | constructor 属性

方法
toLocaleString 方法 | toString 方法 | valueOf 方法

prototype 属性
返回对象类型原型的引用。

objectName.prototype

objectName 参数是对象的名称。

说明
用 prototype 属性提供对象的类的一组基本功能。 对象的新实例“继承”赋予该对象原型的操作。

例如,要为 Array 对象添加返回数组中最大元素值的方法。 要完成这一点,声明该函数,将它加入 Array.prototype, 并使用它。

function array_max( ){
   var i, max = this[0];
   for (i = 1; i < this.length; i++)
   {
   if (max < this[i])
   max = this[i];
   }
   return max;
}
Array.prototype.max = array_max;
var x = new Array(1, 2, 3, 4, 5, 6);
var y = x.max( );
该代码执行后,y 保存数组 x 中的最大值,或说 6。

所有 JScript 固有对象都有只读的 prototype 属性。可以象该例中那样为原型添加功能,但该对象不能被赋予不同的原型。然而,用户定义的对象可以被赋给新的原型。

本语言参考中每个内部对象的方法和属性列表指出哪些是对象原型的部分,哪些不是。

constructor 属性
表示创建对象的函数。

object.constructor

必需的 object是对象或函数的名称。

说明
constructor 属性是所有具有 prototype 的对象的成员。它们包括除 Global 和 Math 对象以外的所有 JScript 固有对象。constructor 属性保存了对构造特定对象实例的函数的引用。 例如:

x = new String("Hi");
if (x.constructor == String)
      // 进行处理(条件为真)。

function MyFunc {
   // 函数体。
}

y = new MyFunc;
if (y.constructor == MyFunc)
      // 进行处理(条件为真)。


toLocaleString,toString,valueOf 方法介绍见前文。

你可能感兴趣的:(JavaScript)