大家好,小弟刚学习javascript这个脚本语言,遇到一个问题,很不解,请大神们帮帮忙解答下,万分感谢tab.prototype.init=function(){ for(var i=0,len=this.span.length;i<len;i++){ this.span[i].index=i; this.span[i].onclick=function(){ this.show(this.index); } } this.show(1); }请问,这段代码块中this分别指向什么?然后这段代码能执行吗
1 回答

聪明的汤姆
TA贡献112条经验 获得超33个赞
指向当前实例,只有这一段代码执行不了,缺少构造函数和实例
完整的代码框架应该如下
// tab构造函数(类)
function tab () {
this.span = []
}
// 原型方法,初始化init
tab.prototype.init = function () {
// 这里面的this都指向当前实例,也就是t
}
var t = new tab();望采纳!
添加回答
举报
0/150
提交
取消