惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
量子位
博客园 - 司徒正美
V
V2EX
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
N
Netflix TechBlog - Medium
L
LangChain Blog
IT之家
IT之家
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - Young.Jiang

Iframe的高级操作 Javascript模拟c#方法重载 设计模式学习:适配器模式 Linq学习笔记(2.3)——DLinq高级操作 Linq学习笔记(2.2)——深入DLinq查询 Linq学习笔记(2.1)——初识 DLinq Linq学习笔记(1.8)——Count、Sum、Min、Max、Average Linq学习笔记(1.7)——First、ElementAt、Any、all Linq学习笔记(1.6)——ToArray、ToList、ToDictionary、OfType Linq学习笔记(1.5)——group、Distinct、Union、Concat、Intersect、Except Linq学习笔记(1.4)——orderby、Reverse() Linq学习笔记(1.3)——Take、Skip Linq学习笔记(1.2)——select Linq学习笔记(1.1)——where 理解JavaScript中的对象 Asp.net Ajax 客户端编程(一)——对JS基本类型的扩展 提高.NET性能的最佳实践 EnterpriseLibrary 3.0的缓存应用程序块 EnterpriseLibrary 3.0的验证应用程序块
Asp.net Ajax 客户端编程(二)——Type 类,面向对象编程的开始
Young.Jiang · 2007-05-11 · via 博客园 - Young.Jiang

    微软的ASP.NET AJAX能写出丰富的,可交互的JavaScript程序,我们可以通过Type类来注册命名空间,类,枚举等,反射对象的内部性质,Type类是Asp.net Ajax的一个很重要类。

Type.registerNamespace(namespacePath)

  注册一个命名空间。

Type.isNamespace(object)

  判断一个对象是否是命名空间。

classInstanceVar.registerClass(typeName, baseType, interfaceTypes)

   注册一个类,typeName,要注册的类名,baseType继承的类,可以为空,interfaceTypes,继承的接口,可以为空。

Type.isClass(type)

  判断一个对象是否是一个类。

typeInstanceVar.getName()

  获取一个对象的类型名。typeInstanceVar是一个类型,可以用Object.getType(对象实例)来获取。

TypeA.isInstanceOfType(instance)

判断一个实例是否是某个类的实例。

typeInstanceVar.registerInterface(typeName)

  注册一个接口。

Type.isInterface(type)

    判断一个类型是否是接口。    

aType.isImplementedBy(typeInstanceVar)

  判断一个类是否实现了接口。typeInstanceVar为类实例

typeInstanceVar.implementsInterface(interfaceType)

  也是判断一个类是否实现了接口,interfaceType为要判断的接口

<script language="javascript" type="text/javascript">
         
//Demo:http://young18.cnblogs.com
         //注册命名空间
         Type.registerNamespace("Arcadia");
         
         Arcadia.IStudent 
= function(){  }
         Arcadia.IStudent.Prototype 
= {
                    Study: 
function(){}
                }

         
//注册接口
         Arcadia.IStudent.registerInterface('Arcadia.IStudent');
         
         
         Arcadia.Student 
= function(name, age) {
                
this._name = name;
                
this._age = age;
             }

         Arcadia.Student.prototype 
= {
               GetName:
function(){
                   
return this._name;
               }
,
               GetAge:
function(){
                   
return this._age;
               }
,
               ToString:
function(){
                   
return "Name:"+this._name+"\nAge:"+this._age;
               }
,
               Study: 
function()return "好好学习"; }
           }
         
         Arcadia.Student.registerClass(
"Arcadia.Student",null,Arcadia.IStudent);
         
         
if(Type.isNamespace(Arcadia))
            alert(
"Arcadia是命名空间");
           
         
if(Type.isInterface(Arcadia.IStudent))
            alert(
"Arcadia.IStudent是接口");
         
         
if(Type.isClass(Arcadia.Student))
            alert(
"Arcadia.Student是一个类");        
         
         
var young = new Arcadia.Student("Young",24);
         
         alert(
"young的类名是:" + Object.getType(young).getName());
         
         
if(Arcadia.Student.isInstanceOfType(young))
            alert(
"young是Arcadia.Student的一个实例");
         
         
if(Arcadia.Student.implementsInterface(Arcadia.IStudent))
            alert(
"Arcadia.Student实现了接口Arcadia.IStudent");
    
</script>

TypeA.inheritsFrom(parentType)

  判断类TypeA是否继承自基类parentType。
   

instanceVar.callBaseMethod(instance, name, baseArguments)

  调用基类方法,instance 为instanceVar的类型,通常为this,name 为要调用的方法名,baseArguments为调用时传入的参数(数组)。

typeInstanceVar.getBaseType()

获取基类类型

typeVar.getInterfaces()

  获取一个基类继承的所有接口。
    

Type.getRootNamespaces()

   获取当前客户端应用程序的所有根命名空间。

typeVar.baseClassName.initializeBase(instance, baseArguments )

   实例化基类,instance为要实例化的基类名,baseArguments 为实例化时传递的参数。

<form id="form1" runat="server">
   
<%--Demo:http://young18.cnblogs.com--%>
   <asp:ScriptManager ID="ScriptManager1" runat="server" />
    
    
<script language="javascript" type="text/javascript">
        
//注册一个命名空间
        Type.registerNamespace("Arcadia");
        Arcadia.Student 
= function(name, age) {
                     
this._name = name;
                     
this._age = age;
                }

        Arcadia.Student.prototype 
= {
                    SetName:
function(name){
                        
this._name = name;
                    }
,
                    GetName:
function(){
                        
return this._name;
                    }
,
                    SetAge:
function(age){
                        
this._age = age;
                    }
,
                    GetAge:
function(){
                        
return this._age;
                    }
,
                    ToString:
function(){
                         
return "Name:"+this._name+"\nAge:"+this._age;
                    }

                }

        
//在Arcadia命名空间下注册一个Student类
        Arcadia.Student.registerClass("Arcadia.Student");
        
        
        
//定义一个Student类的子类GirlStudent
        Arcadia.GirlStudent = function(name, age){
                    
//实例化基类,且其基类属性在GirlStudent中被继承。
                    Arcadia.GirlStudent.initializeBase(this, [name, age]);
                    
this._sex="female";
        }

        Arcadia.GirlStudent.prototype 
= {
                    
//设置性别的只读属性
                    GetSex:function(){
                        
return this._sex;
                    }
,
                    ToString:
function(){
                        
//调用基类方法来重写ToString方法。
                        return Arcadia.GirlStudent.callBaseMethod(this, 'ToString') + "\nSex:美女";
                }

        }
        
        
//注册GirlStudent类,包括和Student的继承关系。
        Arcadia.GirlStudent.registerClass("Arcadia.GirlStudent",Arcadia.Student);
        
       
        
//下面方法类测试上面的对象。
        //分别实例化Student和GirlStudent对象。
        var student=new Arcadia.Student("Young",25);
        
var girlstudent=new Arcadia.GirlStudent("xiaoxue",23);
        
        
//调用实例方法。
        function TestStudent()
        
{
            alert(student.ToString());
        }

        
//改变属性
        function TestChangeStudent()
        
{
            alert(
"改变前:\n"+student.ToString());
            student.SetName(
"Jiang");
            alert(
"改变后:\n"+student.ToString());
        }

        
//用inheritsFrom判断继承关系。
        function TestIfInheritFromStudent()
        
{
            
var isInherit = Arcadia.GirlStudent.inheritsFrom(Arcadia.Student);
            
if(isInherit)
                alert(
"女学生类是从学生类中继承");
            
else
                alert(
"女学生类没有继承自学生类");

        }

        
//测试子类重写后的方法。
        function TestGirlStudent()
        
{
            alert(girlstudent.ToString());
        }

    
</script>
    
<a href="javascript:TestStudent()">测试学生类</a><br />
    
<a href="javascript:TestChangeStudent()">改变学生姓名</a><br />
    
<a href="javascript:TestIfInheritFromStudent()">判断女学生类是否继承自学生类</a><br />
    
<a href="javascript:TestGirlStudent()">测试女学生类</a>
    
</form>


ANamespace.AnEnum.registerEnum(name, flags)

  注册一个枚举

Arcadia.Sex = function(){};
            Arcadia.Sex.prototype 
= 
            
{
                boy:    
"",
                girl:   
""
            }

         Arcadia.Sex.registerEnum(
"Arcadia.Sex");         
         alert(Arcadia.Sex.boy);