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

本文介绍了一个用于评估密码强度的组件实现。通过定义不同长度的密码为弱、中、强等级,该组件使用Type.registerNamespace创建唯一的全局名称,并通过initializeBase方法实现继承行为。

有代码如下:

 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里面,从而实现“继承”

转载于:https://www.cnblogs.com/charrli/archive/2012/05/01/2477496.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值