创建一个简单的Ajax组建(Component)的代码的理解

有代码如下:

 1 /// <reference name="MicrosoftAjax.js"/>
 2 Type.registerNamespace("AjaxEnabled");
 3 
 4 AjaxEnabled.PasswordStrengthComponent = function () {
 5     AjaxEnabled.PasswordStrengthComponent.initializeBase(this);
 6 };
 7 
 8 AjaxEnabled.PasswordStrengthComponent.prototype = {
 9     initialize: function () {
10         AjaxEnabled.PasswordStrengthComponent.callBaseMethod(this, 'initialize');
11     },
12     returnPasswordStrength: function (password) {
13         var strPass = new String(password.toString());
14         if (strPass.length < 5) {
15             return "Weak";
16         }
17         else {
18             if (strPass.length < 8) {
19                 return "Medium";
20             }
21             else {
22                 return "Strong";
23             }
24         }
25     },
26     dispose: function () {
27         AjaxEnabled.PasswordStrengthComponent.callBaseMethod(this, 'dispose');
28     }
29 };
30 
31 AjaxEnabled.PasswordStrengthComponent.registerClass('AjaxEnabled.PasswordStrengthComponent', Sys.Component);
32 
33 if (typeof (Sys) !== 'undefined')
34     Sys.Application.notifyScriptLoaded();

1. 第2行Type.registerNamespace()实际上是为类创造一个unique的全局名字

2. 第5行的initializeBase的方法是类库针对Function进行的拓展,而Function的一个别名是Type。也就是说,第5行在声明passwordStrengthComponent为一个function的时候,就已经自动获得了initializeBase方法。

3. 第5行的initilizeBase()方法虽然出现得比较早,但是执行时间晚于第31行的registerClass()方法。

4. 第31行的registerClass,其作用是把‘AjaxEnabled.PasswordStrengthComponent'类给“注册”到Sys名空间内,同时指明其base类是Sys.Component。但是此时AjaxEnabled.PasswordStrengthComponent类并没有继承Sys.Component,因为实际的“继承”发生在第5行的。

5. 在AjaxEnabled.PasswordStrengthCompent被实例化时,其内部的initializeBase方法将会被执行,此时Sys.Component内部的property将会被逐一复制到AjaxEnabled.PasswordStrengthComponent里面,从而实现“继承”

posted on 2012-05-01 00:37  李志鹏  阅读(480)  评论(0)    收藏  举报

导航