I'm trying to keep my JS code nice and OOP from now on. I think I've gotten the hang of it, could you check this code below and let me know if there are any glaring errors?
/*------- collapser -------*/
function Collapse(){
var $this = this;
$(document).ready(function(e){
$this.setEvents($this, e);
});
}
Collapse.prototype.setEvents = function($this, e){
$('.collapse>div').hide();
$('.collapse>h1').click(function(e){
$this.show($this, e);
});
}
Collapse.prototype.show = function($this, e){
var element = e.originalEvent.srcElement.parentNode;
$(element).children("div").slideToggle();
}
var collapse = new Collapse();
One question, is there a better way to get the class instance without passing $this down everytime? I'd love to hook events like this:
$(document).ready(setEvents);
And in the setEvents
function, have this
be an instance of the "collapser." Is there a way to do this?
Thanks in advance!
Thanks everyone.
It turned out to be OOP enough for what I wanted (singleton web-apps / widgets).
I've since moved on to AngularJS
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.