论坛首页 Web前端技术论坛

JavaScript对象expando属性 强大、优美的地方

浏览 3175 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-12-03  
下面是一个会员登录对话框,要求XHR验证用户身份后返回用户名称,显示在对话框标题上
//JumperDailog为对话框类,注意title属性是待格式化文本,{0}代表将来的“用户名称”的位置
JumperDailog = Ext.extend(Ext.Window, {
	width : 625,
	height : 332,
	title : '{0}已成功登录',
	style : "text-align:center",
	initComponent : function(){
		alert(this.title+this.UserName);
		this.setTitle(String.format(this.title, this.UserName));//this.UserName从配置项对象上得到
	},
	html : {tag : 'div', children :[
         ............
        ]}
});


登录成功后:
				(new JumperDlg({
					UserName : 'Jack' //这里应是XHR返回的用户名
				})).show()

UserName的属性经过JumperDailog的构造器内apply函数实现复制,挂到JumperDailog身上(expando的体现),
然后initComponent + setTitle的方法实现了 “Jack已成功登录”之文本,
--一切貌合神离,一切无须多讲
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics