[ASP.NET Ajax] ECMAScript基础类以及Asp.net Ajax对类<Obje

2019-09-14 07:30:26刘景俊


在Asp.NET Ajax中微软为了将整个类库扩展的更适合.NET的开发人员,在原始的Object对象中扩展了两个方法:

Object.__typeName="Object";

Object.getType=function(b){

  var a=b.constructor;

  if(!a||typeof a!=="function"||!a.__typeName||a.__typeName==="Object")return Object;

  return a

};

Object.getTypeName=function(a){

  return Object.getType(a).getName()

};

从上面我们可以看到,Object.getType(type)这个静态方法可以获取一个类的对象,而Object.getTypeName(type)通过调用[prototype]Type.getName()而获取类的名(包括命名空间)。这没有什么好说的,下面来看一下测试:

        <asp:ScriptManager ID="ScriptManager1" runat="server" />

        <div>

        <script language="javascript">

        Type.registerNamespace("NExplus");

        //define a class;

        NExplus.ObjectTest=function(name){

          this._name=name;

        }

        NExplus.ObjectTest.prototype.getName=function(){

          return (this._name === undefined) ? null : this._name;

        }

        NExplus.ObjectTest.prototype.setName=function(name){

          this._name=name;

        }

        //register class;

        NExplus.ObjectTest.registerClass("NExplus.ObjectTest");

        

        NExplus.TestObject=function(){

          var a=new NExplus.ObjectTest("Test is success?");

          document.write(a.getName());

          document.write("<br/>");

          a.setName("Success!");

          document.write(a.getName());

          document.write("<br/>");

          document.write(Object.getTypeName(a)+"<br/>")