日期:2014-05-16  浏览次数:20469 次

一个prototype的问题。。
function Calendar()
{
alert(1);
this.bindData();
}
//原型方法
Calendar.prototype.bindData=function(){
alert("bindData");
}
Calendar();
这么写this.bindData()有什么作用?
------解决方案--------------------
this不是说你声明在哪个function就是指那个作用域。
this要看你的function是怎样被使用的
您的例子中,
Calendar();
显然,this被指到了window上
so,会报undefine错误的
------解决方案--------------------
function Calendar()
{
if(this.constructor!=Calendar){
new Calendar();
}
    this.bindData();
}
//原型方法
Calendar.prototype.bindData=function(){
    alert("bindData");
}
Calendar();
你那样this指向的是window  所以方法无效

------解决方案--------------------
因为他写错了,或漏写了new
new Calendar();