想问大家一个问题,为什么看到别人写的类,属性都挂在this上?不可以放在方法里吗?比如
function GetMusic($content) { this.content = $content; this.title = this.content.find(".title");
}
GetMusic.prototype.changTitle = function() { this.title.toggleClass("on");
}
}为什么不这样写
function GetMusic($content) { this.content = $content;
}
GetMusic.prototype.changTitle = function() { var title = this.content.find(".title");
title.toggleClass("on");
}
}就是为什么有时候这个变量只用一次也挂在实例上,就是用this挂起来。那么其实:
第一种:
function GetMusic($content) { this.content = $content; this.title = this.content.find(".title");
}第二种
function GetMusic($content) { this.content = $content;
}
GetMusic.prototype.changTitle = function() { this.title = this.content.find(".title");
}
}第三种
function GetMusic($content) { this.content = $content;
}
GetMusic.prototype.changTitle = function() { var title = this.content.find(".title");
}
}上面的三个title变量有什么不一样?对性能什么的有什么印象?一般什么情况下用哪种?
慕容3067478
qq_花开花谢_0
随时随地看视频慕课网APP
相关分类