(function() {
let create = {
init() {
this.cacheDom();
this.bindEvent();
},
bindEvent() {
$(this.$content_buttons).click(function() {
create.preformAction(this);
});
},
cacheDom() {
this.$content_buttons = $('.content_button');
},
preformAction(el) {
alert('its class is ' + $(el).attr('class'));
}
};
create.init();
}());
考虑上面的代码,我可以通过执行此 create.performAction 访问 create 对象的 performAction 方法。还有其他办法吗?提前致谢。
最佳答案
尝试一下:
(function() {
let create = {
init() {
this.cacheDom();
this.bindEvent();
},
bindEvent() {
$(this.$content_buttons).click(function() {
create.preformAction(this);
});
},
cacheDom() {
this.$content_buttons = $('.content_button');
},
preformAction(el) {
alert('its class is ' + $(el).attr('class'));
}
};
create.init();
}());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="content_button class1">click1</button>
<button class="content_button class2">click2</button>
关于javascript - 如何访问事件绑定(bind)中的对象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52639469/