87,991
社区成员
发帖
与我相关
我的任务
分享
$(function(){
var GiantCorp={};
GiantCorp.RegPage={
FORM_ID:'reg-form',
OUTPUT_ID:'reg-result',
init:function(){
GiantCorp.RegPage.fromEl=$("#"+GiantCorp.RegPage.FORM_ID)
GiantCorp.RegPage.fromEl.click(function(e){
alert(123);
});
}
}
GiantCorp.RegPage.init()
})
$(function(){
var GiantCorp={};
GiantCorp.RegPage={
FORM_ID:'reg-form',
OUTPUT_ID:'reg-result',
init:function(){
this.fromEl=$("#"+this.FORM_ID)
this.fromEl.click(function(e){
alert(123);
})
}
}
GiantCorp.RegPage.init()
})
//在click页面上的a元素时执行init
$('a').click(GiantCorp.RegPage.init);
//然后在控制台里打印一下GiantCorp.RegPage对象
console.log(GiantCorp.RegPage.init)
//结果:
//如果是按照第一种模式,那么GiantCorp.RegPage正常生成一个单例即:
//显示Object {FORM_ID: "reg-form", OUTPUT_ID: "reg-result", init: function, fromEl: b.fn.b.init[0]}
//如果是用含this的方式,那么GiantCorp.RegPage初始化失败(无法新增fromEl属性并给jquery对象
//添加click事件),因为这种情况init在执行的时候this已经变成了a元素。
//显示Object {FORM_ID: "reg-form", OUTPUT_ID: "reg-result", init: function}
====js里this无论在何时,都是要谨慎使用的====