[ASP. net ajax] Function object and Type Class Method Introduction

Source: Internet
Author: User

The last time we learned about JavaScript Microsoft AJAX Library in general, it seems a little abstract because of temporary issues. This time, we will certainly use some examples to Make Asp more intuitive. NET Ajax script library is a very important class Type. This class provides some reflection methods for extending object-oriented programming. Through this class, we can register basic types like. NET (such as namespaces, classes, enumerations, and so on. This Type class inherits from window as a Global Type and does not belong to any namespace. Next let's take a look at some basic methods in Type and how to implement some methods...

Before recognizing the Type class, we should first take a look at a very important object, that is, the Function object. Function objects are JavaScript internal objects, such as Date, Array, String, and RegExp, Math, and Error) all belong to this type. You can use new to instantiate an object. In addition to our common JavaScript declarative functions, we can also use the following method to construct our Function object: var myFunction = new Function (arguments, statement ). Because I am not very familiar with the script, I am familiar with prototype (however, I heard that a prototype object is provided to specify the type of the object to be created, then, use the method of this prototype object to create more objects of the same type. The original model mode is not very familiar with the object creation mode. However, after a simple test, if you perform prototype extension on the Function, you can declare the object through new and then reference it. You can also directly call the Function. (method or function ). If you directly use Function. the uName can only be extended through the Function. the uName call reminds me of this. NET static method (C #: static; VB: share), with these, let's look at Asp. NET Ajax:
Copy codeThe Code is as follows:
Function. _ typeName = "Function ";
Function. createCallback = function (B, ){
Return function (){
Var e = arguments. length;
If (e> 0 ){
Var d = [];
For (var c = 0; c <e; c ++)
D [c] = arguments [c];
D [e] =;
Return B. apply (this, d)
}
Return B. call (this,)
}
};
Function. createDelegate = function (a, B ){
Return function (){
Return B. apply (a, arguments)
}
};
Function. emptyFunction = Function. emptyMethod = function (){};
From the above we can see that Function extends an attribute (_ typeName) and several methods (we can regard it. NET static object to call, if you use new Function (); then call it, you will get "undefined"; maybe this is the most basic thing in JavaScript, however, I have never used such objects to write scripts or the system has never learned JavaScript, so don't laugh at me. I am learning ...), I also found a relatively rare call/apply. I searched the internet and found that <fully understand javascript caller, callee, call, apply concept> here is a detailed introduction. Function. createCallback ("callback method", "callback argument") is related to callback. Function. createDelegate ("object usually uses this", "method of execution"), that is, through Function. the proxy method created by createDelegate inherits the B method to complete the entire proxy process (I do not know if it is correct !); Function. emptyFunction/Function. emptyMethod should be prepared for interfaces and virtual functions.

The Type object inherits the Function object and is further extended from the Function object. Here we will not discuss the implementation process of Type. Today we will mainly look at some important methods in Type. There are a total of 22 Methods in Type. Here we will introduce them one by one:

. [Prototype] Type. callBaseMethod (instance, name, baseArguments)
Note:
This function mainly calls the methods in the base function, which is equivalent to the base in C #. This function is used only when inheriting the function.
Parameters:
Instance: the current instance of the base function to be called. this is usually used;
Name: name of the base function to be called, represented by a string;
BaseArguments: the parameter of the base function to be called.

. [Prototype] Type. getBaseMethod (instance, name)
Note:
REFERENCE The Method Instance of the base Function. If you want to use the Function. the call function calls a method, which is your choice. However, I think the difference with the previous function is not very big and confused.
Parameters:
Instance: it seems that this function has not been called. The implementation of the previous function obtains the function through this function. This parameter may be reserved for the instance parameter in 1, I don't know why they are doing this. How can they be a master of freedom!
Name: Of course, it is the name of the method to be referenced.

. [Prototype] Type. getBaseType ()
Note:
It actually returns the value of "typeof this. _ baseType". If it is "undefined", null is returned.

. [Prototype] Type. getInterfaces ()
Note:
The interface object Array implemented by the current instance can be obtained as an Array object.

. [Prototype] Type. getName ()
Note:
Returns the name of the current object, including the namespace and class name. If it is "undefined", return "".

. [Prototype] Type. implementsInterface (interfaceType)
Note:
If the current class has implemented all definitions in the interfaceType interface, true is returned; otherwise, false is returned. In this way, the promise of inheriting all methods or attributes must be fulfilled in. NET :)

. [Prototype] Type. inheritsFrom (parentType)
Note:
Call this method to determine whether the current instance inherits from the parentType class, such as var isInherited = ClassA. inheritsFrom (ClassB). If the current instance inherits from parentType, true is returned; otherwise, false is returned.

. [Prototype] Type. initializeBase (instance, baseArguments)
Note:
This is a very important function. You can use this function to initialize the constructor of the base function. You can also register yourself as a base function. Here, instance is the object that initializes the base class, usually using this; baseArguments as the parameter of the base function constructor, which can be null.

. [Prototype] Type. isImplementedBy (typeInstance)
Note:
This is the opposite of 6. It is used to determine whether typeInstance has implemented all definitions in the interface. If so, true is returned; otherwise, false is returned.

. [Prototype] Type. isInstanceOfType (instance)
Note:
Determines whether a class is the current instance of the instance. It is mainly used to determine whether the class is an instance of its own class (subclass. Returns true/false.

. [Prototype] Type. registerClass (typeName, baseType, interfaceTypes)
Note:
At first glance, we can see that a class is registered. baseType and interfaceTypes are both optional. At the same time, like. NET, there is at most one baseType, and the interface doesn't matter. This method is used before the initialization of a class. If there is a baseType, the constructors of the base function must be instantiated through Type. initializeBase in the first line.

. [Prototype] Type. registerInterface (typeName)
Note:
Register a class as an interface. The interface contains any processing functions.

. [Prototype] Type. resolveInheritance ()
Note:
This method is very fun. Copy the attributes of the base class to extend prototype of the current class. It can be used to expand reflection in object-oriented programming.

. Type. getRootNamespaces ()
Note:
Static functions (I don't know how others call them, but I just want to call them static functions). This method can be used to obtain the Array of all namespaces (that is, an Array is returned, including all namespaces ).

. Type. isClass (type)
. Type. isInterface (type)
. Type. isNamespace (type)

. Type. parse (typeName, ns)
Note:
This allows you to create an object that uses Type. GetType ("type, ns"); and Invoke (); In. NET to reflect and instantiate a class. Ns namespace is optional. If the class is not in a namespace (for example, Type class), typeName can be null.

. Type. registerNamespace (namespacePath)
Note:
Register a namespace.

. [Prototype] Type. registerEnum (name, flag)
Note:
Register as Enumeration type, flag determines whether it is bit type, optional.

. Type. isEnum (type)
. Type. isFlags (type)

The example still does not contain Hu, and the original understanding seems vague. In order to give people a more intuitive understanding, I put some examples here (html file ):


If you want to test the Enum, use the <ScriptManager/> space reference script. The script in the example does not have the Number class extension, so you cannot test the Enum. A few hours later, however, I have learned a lot about it. I am increasingly confused when writing this article. If there are any mistakes, please point out that it is necessary to allow Bill Gate to become richer first and then drive us to become richer, are you sure you want to be prosperous together :)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.